zoukankan      html  css  js  c++  java
  • 使用WebClient进行上传文件 [ZT]

       private bool UploadFile(string source,string targetUrl,NetworkCredential networkCredential)
            
    {
                FileStream streamSource;
                Stream streamTarget;
                WebClient client 
    = new WebClient();
                client.Credentials 
    = networkCredential;
                
    try
                
    {
                    streamSource 
    = File.OpenRead(source);
                }

                
    catch (Exception err)
                
    {
                    
    return false;
                }

                
    try
                
    {
                    Uri url 
    = new Uri(targetUrl);
                    streamTarget 
    = client.OpenWrite(url, "PUT");
                }

                
    catch (Exception err)
                
    {
                    
    return false;
                }

                
    try
                
    {
                    
    long num = 0;
                    
    int count = 0;
                    
    byte[] buffer = new byte[512];
                    
    while (num < streamSource.Length)
                    
    {
                        count 
    = streamSource.Read(buffer, 0512);
                        streamTarget.Write(buffer, 
    0, count);
                        num 
    += count;
                        
                    }

                    streamTarget.Close();
                    streamSource.Close();
                    
    return true;
                }

                
    catch (Exception err)
                
    {
                    
    return false;
                }

            }
     
  • 相关阅读:
    [LoadRunner]UTF8字符格式
    Python的数字类型及其技巧
    Python的数据类型总结
    理解Python的迭代器
    一篇文章让你彻底搞清楚Python中self的含义
    Python中类的运算符重载
    深入理解Python中的生成器
    模态窗口点滴
    ASP.NET 防盗链的实现[HttpHandler]
    ASP.NET C# DataTable与Excel互操作示例
  • 原文地址:https://www.cnblogs.com/RobotTech/p/928856.html
Copyright © 2011-2022 走看看