Uploading files (up to 10Mb file size) with ASP.NET webAPI

I work on a VS2012 MVC4 project containing a Web API controller. This project will be published on an IIS server.

I need to allow users to upload files. The problem is a web API is limited up to 4MB maximum file size upload. I read (for example here: http://blogs.msdn.com/b/henrikn/archive/2012/03/01/file-upload-and-asp-net-web-api.aspx) that we can extend this limitation by self hosting the web API (in this case file upload up to 2GB). I don’t want self host my webAPI because I want to host it on my IIS web server so I think this is not suitable for my situation, right? So what can I do for uploading files bigger than 4MB?

If possible I search for an HTML5 solution (with drag’n drop).

So far none of the solutions I found allows me to accomplish this.

Thanks for your help.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Probably it was not clear but actually the blog URL is referring to IIS. You need to look for the following 2 settings in Web.config to increase the upload size:

Note maxRequestLength is in kbytes:

<system.web>
  <httpRuntime targetFramework="4.5" maxQueryStringLength="" maxRequestLength="" maxUrlLength="" />

Note maxAllowedContentLength is in bytes:

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="" maxQueryString="" maxUrl=""/>

Method 2

If you read it carefully, it says “ASP.NET has a maximum limit of 2G in terms of file size that you can upload.” So basically when hosted in ASP.NET/IIS you will be able to receive files up to 2Gbs. What you have to do is change some default values in web.config.

Check this out: https://stackoverflow.com/a/7154363/2517785

Method 3

You can check this

 int MaxContentLength = 1024 * 1024 * 10; //Size = 10 MB
if (postedFile.ContentLength > MaxContentLength)
{


}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x