I had it working.. but I noticed once the files I was uploading get bigger (around 4000k) the controller would not be called..
So I added in chunking which fixed that problem.. but now when I open the file its full of garbage characters...
So what is the correct way to upload large files with plupload/MVC 4 ?
Here is my current code
$(document).ready(function () {
var uploader = new plupload.Uploader({
runtimes: 'html5',
browse_button: 'pickfiles',
container: 'container',
// max_file_size: '20000mb',
url: '@Url.Action("Upload", "Home")',
chunk_size: '4mb',
//filters: [
// { title: "Excel files", extensions: "xls,xlsx" },
// { title: "Text files", extensions: "txt" }
//],
multiple_queues: true,
multipart: true,
multipart_params: { taskId: '' }
});
and the controller
[HttpPost]
public ActionResult Upload(int? chunk, string name, string taskId)
{
string filePath = "";
var fileUpload = Request.Files[0];
var uploadPath = Server.MapPath("~/App_Data/Uploads");
chunk = chunk ?? 0;
string uploadedFilePath = Path.Combine(uploadPath, name);
var fileName = Path.GetFileName(uploadedFilePath);
try
{
using (var fs = new FileStream(filePath, chunk == 0 ? FileMode.Create : FileMode.Append))
{
var buffer = new byte[fileUpload.InputStream.Length];
fileUpload.InputStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
//Log to DB for future processing
InstanceExpert.AddProcessStart(filePath, Int32.Parse(taskId));
}
In web.config you need these (2GB all around):
I had this problem and found the solution based on Jonathan's code here. If you want to upload a large file, something like 1Gbyte video file, you have to chuck the file and send it through several request (one request gives time out). first you set the max limit for client and server side in Web.config as discussed in other answers.
and
then chunk the file, and send each chuck, wait for response and send the next chunk. here is the html (VideoDiv work as upload panel), javascript (jQuery) and controller code.
Javascript code to chuck, call controller and update progressbar:
and here is the upload controller to store the chucnk in ("App_Data/Videos/Temp") and later merge them and store in ("App_Data/Videos"):
However, if two users at same time upload files with same name, there will be some problem, and you have to handle this issue. By reading responseText, you can catch some error and exception and trim it.
Current Version
According to the detailed error description of IIS 8.0, which is the version I used at the time I wrote this answer, you need to verify the configuration/system.webServer/security/requestFiltering/requestLimits@maxAllowedContentLength setting in the ApplicationHost.config or Web.config file. That means you need to include:
inside configuration/system.webServer/security/requestFiltering tag tree. Just in case you lack the imagination to visualize where it goes, the full code block goes like the following:
Visual Studio 2010/.Net Framework 4 and Before
It is also possible that legacy web applications created with VS2008/10 and/or .Net Framework 3.5/4 may still be looking for this configuration via configuration/system.web/httpRuntime@maxRequestLength, but as evidenced by the linked page, it is no longer available, although HttpRuntime Class, which doesn't apply to this scenario, still exists since .Net Framework 1.1. If this is the case, you need to include:
inside configuration/system.web/httpRuntime tag tree. Once again, just in case you lack the comprehensibility to figure out where it gets inserted, the full code block looks something like the following:
The file size number is just an arbitrary number (20,000 MB – not 20 GB, which would rather be 21,474,836,480) to show as a demo. Unless you're coding the website for a tight security group who has a need to upload large files, you shouldn't allow this big of a file size being uploaded to your web server.