zoukankan      html  css  js  c++  java
  • WebClient 上传下载用法

    今天学习了一下WebClient的上传、下载。

    具体用法如下。 我们先在IIS网站中建立一个文件夹,此处为"Mp3",并设置此文件夹相关读写权限。

    例1:使用WebClient中的UploadFile方法上传文件。代码如下。使用此方法需要将上传的文件夹权限设置为IIS来宾账户允许读写。

            const string UploadFilePath = "http://localhost/Mp3/";
            
    /// <summary>
            
    /// 上传文件
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            private void btnUpload_Click(object sender, EventArgs e)
            {
                
    try
                {
                    WebClient client 
    = new WebClient();
                    
    string uploadFilePath = UploadFilePath + DateTime.Now.ToString("yyMMddHHmmssff"+ ".txt";
                    
    /*
                     * PUT:如果文件名在客户端确定,就使用PUT
                     * POST:如果文件名在服务器端确定,就使用POST
                     
    */
                    client.UploadFile(uploadFilePath, 
    "PUT", txbFile.Text);
                }
                
    catch (Exception ex)
                {
                    
    throw ex;
                }
            }

     例2:使用WebClient的UpLoadData上传文件

            /// <summary>
            
    /// 上传文件 使用UploadData
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                
    try
                {
                    
    string uploadFilePath = UploadTestFilePath + DateTime.Now.ToString("yyMMddHHmmssff"+ ".txt";
                    WebClient client 
    = new WebClient();
                    FileStream fStream 
    = new FileStream(txbFile.Text, FileMode.Open, FileAccess.Read);
                    BinaryReader bReader 
    = new BinaryReader(fStream);
                    
    byte[] uploadByte = bReader.ReadBytes(Convert.ToInt32(fStream.Length));
               
                    
    try
                    {
                        
    byte[] responseArray = client.UploadData(uploadFilePath, "PUT", uploadByte);
                        
    string returnValue = Encoding.GetEncoding("gb2312").GetString(responseArray);
                    }
                    
    catch (Exception ex)
                    {

                        
    throw ex;
                    }
                    
    finally
                    {

                        fStream.Close();
                        fStream.Dispose();
                        client.Dispose();
                    }
                }
                
    catch (Exception ex)
                {
                    
    throw ex;
                }
            }

    例3:使用WebClient的Write方法上传。 

            /// <summary>
            
    /// 上传(Stream)
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            private void button3_Click(object sender, EventArgs e)
            {
                
    try
                {
                    
    string uploadFilePath = UploadTestFilePath + DateTime.Now.ToString("yyMMddHHmmssff"+ ".txt";
                    WebClient client 
    = new WebClient();
                    client.UseDefaultCredentials 
    = true;
                    client.Credentials 
    = CredentialCache.DefaultCredentials;
                    FileStream fStream 
    = new FileStream(txbFile.Text, FileMode.Open, FileAccess.Read);
                    BinaryReader bReader 
    = new BinaryReader(fStream);
                    
    byte[] uploadByte = bReader.ReadBytes(Convert.ToInt32(fStream.Length));
                    Stream uploadStream 
    = client.OpenWrite(uploadFilePath, "PUT");
                    
    try
                    {
                        
    if (uploadStream.CanWrite)
                        {
                            uploadStream.Write(uploadByte, 
    0, uploadByte.Length);
                        }
                    }
                    
    catch (Exception ex)
                    {

                        
    throw ex;
                    }
                    
    finally
                    {

                        fStream.Close();
                        fStream.Dispose();
                        uploadStream.Close();
                        uploadStream.Dispose();
                        client.Dispose();
                    }
                }
                
    catch (Exception ex)
                {
                    
    throw ex;
                }
            }

    例4:打开文件,不需要验证。此处使用的是匿名账户。

            /// <summary>
            
    /// 打开文件 无授权
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            private void btnOpen_Click(object sender, EventArgs e)
            {
                
    string serverFilePath = "http://localhost/Mp3/11060715414724.txt";
                WebClient client 
    = new WebClient();
                Stream stream 
    = client.OpenRead(serverFilePath);
                StreamReader reader 
    = new StreamReader(stream);
                rtbData.Text 
    = reader.ReadToEnd();
            }

     例5:使用本地账户验证。

    此处需要将IIS网站授权勾选成“集成widdows验证”。

            /// <summary>
            
    /// 自定义授权
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {
                WebClient client 
    = new WebClient();
                NetworkCredential credential 
    = new NetworkCredential(@"ckt""ckt");
                client.Credentials 
    =
     credential;
                
    string serverFilePath = "http://localhost/Mp3/11060715414724.txt";
                Stream stream 
    = client.OpenRead(serverFilePath);
                StreamReader reader 
    = new StreamReader(stream);
                rtbData.Text 
    = reader.ReadToEnd();            
            }

    例6:下载文件

            /// <summary>
            
    /// 下载文件
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            private void btnDownload_Click(object sender, EventArgs e)
            {
                
    try
                {
                    WebClient client 
    = new WebClient();
                    
    string serverFile = "http://localhost/Mp3/11060715414724.txt";
                    WebRequest request 
    = WebRequest.Create(serverFile);
                    client.DownloadFile(serverFile, 
    @"d:\11.txt");
                }
                
    catch (Exception ex)
                {
                    
    throw ex;
                }
            }

    附:

    测试程序代码: /Files/scottckt/WebClientStudy.rar

    测试程序界面如下。

     

  • 相关阅读:
    错误解决记录-------------验证启动HDFS时遇到的错误
    Spark环境搭建(一)-----------HDFS分布式文件系统搭建
    Synergy简单使用小记
    python基础一 ------排序和查找算法
    Scrapy基础(十四)————Scrapy实现知乎模拟登陆
    Scrapy基础(十四)————知乎模拟登陆
    Scrapy基础(十三)————ItemLoader的简单使用
    Scrapy基础(十二)————异步导出Item数据到Mysql中
    简单python爬虫练习 E站本爬取
    7-4 jmu-Java&Python-统计文字中的单词数量并按出现次数排序 (25分)
  • 原文地址:https://www.cnblogs.com/scottckt/p/2074486.html
Copyright © 2011-2022 走看看