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

            }
     
  • 相关阅读:
    【证明】—— 二叉树的相关证明
    ubuntu编译安装opencv
    【换句话说】【等价描述】—— 定义及概念的不同描述
    YOLOv3训练自己的数据
    【证明】【一题多解】布尔不等式(union bound)的证明
    机器视觉:MobileNet 和 ShuffleNet
    keras图像风格迁移
    【算法导论】【排序】—— 计数排序(counting sort)
    【等价转换】—— min/max 的转换与互相转换
    卷积神经网络特征图可视化(自定义网络和VGG网络)
  • 原文地址:https://www.cnblogs.com/RobotTech/p/928856.html
Copyright © 2011-2022 走看看