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

            }
     
  • 相关阅读:
    转战博客园
    C++虐恋:MBCS安装失败导致的四天误工
    Servlet 3.0 新特性详解 (转载)
    数据库连接池proxool的两种使用方式
    java异常处理中的细节
    InvocationTargetException异常的深入研究-servlet的setAttribute与getAttribute
    如果我是一个全栈极客,那么,下一步该怎么走?
    C++基础与提高 001
    用户及文件权限管理
    命令行操作体验
  • 原文地址:https://www.cnblogs.com/RobotTech/p/928856.html
Copyright © 2011-2022 走看看