Why is WebClient.UploadFileAsync not working with

2019-07-27 03:49发布

问题:

I am uploading a file from a C# application to an ASP.Net website, both written by me so I have access to the code.

But, it is working for a text file (1KB) but not for a MOV file (77MB).

In both cases, I am using UploadProgressChanged to get the progress. The TXT file goes to 100% while the MOV file goes only till 50%. When done, I only find the TXT file saved on the server but not the MOV file.

Why is this happening? How can I get it to work?

Windows App Code - C#

Client.UploadFileAsync(new Uri("http://localhost:xxxxxx/Default.aspx"),  "c:\\1.MOV");

Default.aspx Code - VB

Protected Sub Page_Load(...) Handles Me.Load
    If Request.Files IsNot Nothing Then
        If Request.Files.Count > 0 Then
            Request.Files(0).SaveAs(Server.MapPath("~/1.mov"))
            Response.Write("File saved at: " + Server.MapPath("~/1.mov"))
        Else
            Response.Write("At least one file should be sent")
        End If
    Else
        Response.Write("No file received")
    End If
End Sub

回答1:

You can change the default limit (4mb) in a localized way by dropping a web.config file in the directory where your upload page lives. That way you don't have to allow huge uploads across your whole site(doing so would open you up to a certain kinds of attacks).

Here's an example from a production app. This one is set for 100mb:

<configuration>
  <system.web>
  <httpRuntime maxRequestLength="100000" executionTimeout="600" />
  </system.web>
</configuration>


回答2:

Exception throws when the specified value is less than 1 kilobytes and greater than 4194304 kilobytes (4 GB).