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

    测试程序界面如下。

     

  • 相关阅读:
    Java实现网易163邮箱好友通讯录的解析功能(带源码)
    wordpress优化第四招 修改评论模板,留住客户,让评论在新的页面打开。
    wordpress优化 使用SAE提供的jquery.js替代wordpress原生的
    出售wordpress的淘宝客主题一套
    做了一个可以生成在线mp3 flash播放器的网站
    wordpress优化第三招 开启gzip减少网页流量
    20多个常用的免费WebService接口
    wordpress优化第一招 压缩css和js减少流量提高博客速度(尤其适用SAE)
    Linux学习笔记10常用操作命令(useradd命令、passwd 命令)
    Linux学习笔记08linux文本处理(cat命令、more命令、head命令、tail命令)
  • 原文地址:https://www.cnblogs.com/scottckt/p/2074486.html
Copyright © 2011-2022 走看看