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;
                }

            }
     
  • 相关阅读:
    清空收缩数据库日志文件的方法
    JavaScript中的剪贴板的使用(clipboardData)
    location.search在客户端获取Url参数的方法
    JS之onkeypress,onkeydown,onkeyup区别
    网页中的meta标签的作用
    SQL优化条
    JS简洁经典滚动上下与左右
    结构体
    static修饰符
    Solaris内存监控 & solaris查看内存使用情况
  • 原文地址:https://www.cnblogs.com/RobotTech/p/928856.html
Copyright © 2011-2022 走看看