使用 WebClient 异步上载文件

来源:岁月联盟 编辑:exp 时间:2011-12-12

代码比较简单,但需要有几点要注意的地方:

VB.NET 代码
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.ProgressBar1.Value = 0
    If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
      Dim fileName As String = OpenFileDialog1.FileName
      Dim client As WebClient = New WebClient()
      Dim querystring As System.Collections.Specialized.NameValueCollection = New System.Collections.Specialized.NameValueCollection()
      querystring.Add("name", "mengxianhui")
      querystring.Add("from", "net_lover")
      client.QueryString = querystring
      Dim uri As Uri = New Uri("http://www.mengxianhui.com:802/Upload.aspx")
      AddHandler client.UploadFileCompleted, AddressOf UploadFileCompletedCallback
      AddHandler client.UploadProgressChanged, AddressOf UploadProgressCallback
      client.UploadFileAsync(uri, "POST", fileName)
    End If
  End Sub

  Private Sub UploadFileCompletedCallback(sender As Object, e As UploadFileCompletedEventArgs)
    MessageBox.Show("上传完毕")
  End Sub

  Private Sub UploadProgressCallback(sender As Object, e As UploadProgressChangedEventArgs)
    Me.ProgressBar1.Maximum = 100
    Me.ProgressBar1.Minimum = 0
    Me.ProgressBar1.Value = e.ProgressPercentage
  End Sub
接收文件的代码

Upload.aspx 代码
<%@ Page Language="C#" AutoEventWireup="true" %>

<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {   
    if (Request.Files != null)
    {
      if (Request.Files.Count > 0)
      {
        //作为测试,获取的QueryString参数作为文件名的前缀
        String fileName = System.IO.Path.GetFileName(Request.Files[0].FileName);
        Request.Files[0].SaveAs(Server.MapPath("~/" + Request.QueryString["name"] + "_" + Request.QueryString["from"] + "_" + fileName));
      }
    }
  }
</script>
对于大文件上传,需要在web.config文件里面设置最大的请求长度

Web.config 代码
<configuration>
  <system.web>
    <httpRuntime maxRequestLength="600000" executionTimeout="600" />
  </system.web>
</configuration>

 如果文件在上传到50%就立即完成了,则说明服务器端可能出现了问题。请检查文件是否真的保存到了服务器。

C# 代码
private void button5_Click(object sender, EventArgs e)
{
  progressBar1.Value = 0;
  if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  {
    String fileName = openFileDialog1.FileName;
    WebClient client = new WebClient();
    Uri uri = new Uri("http://www.mengxianhui.com:802/Upload.aspx");
    System.Collections.Specialized.NameValueCollection querystring  = new System.Collections.Specialized.NameValueCollection();
    querystring.Add("name", "mengxianhui");
    querystring.Add("from", "net_lover");
    client.QueryString = querystring;
    client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged);
    client.UploadFileCompleted += new UploadFileCompletedEventHandler(client_UploadFileCompleted);
    client.UploadFileAsync(uri, "POST", fileName);
  }
}

void client_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
{
  MessageBox.Show("上传完毕");
}

void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
  progressBar1.Maximum = 100;
  progressBar1.Minimum = 0;
  progressBar1.Value = e.ProgressPercentage;
}

作者 孟宪会