I am trying to get the blueimp jQuery-File-Upload component working in my MVC application.
Here is my code so far.
JavaScript:
$('#fileupload').fileupload({ url: '@Url.Action("AddAttachment", "File")', dataType: 'json', progressall: function (e, data) { var progress = parseInt(data.loaded / data.total * 100, 10); console.log(progress); }, add: function(e, data) { data.submit(); }, done: function (e, data) { $.each(data.result.files, function (index, file) { $('<p/>').text(file.name).appendTo(document.body); }); } });
Controller:
[HttpPost] public JsonResult AddAttachment() { var files = new List<FileAttachmentFileModel>(); try { if (Request.Files.Count > 0) { using (var dbContext = new vegaEntities()) using (var transaction = dbContext.Database.BeginTransaction()) { for (int i = 0; i < Request.Files.Count; i++) { HttpPostedFileBase file = Request.Files[i]; if (file.ContentLength > 0) { FileAttachment fileAttachment = new FileAttachment { Id = Guid.NewGuid(), FileName = file.FileName, ContentType = file.ContentType, DateAdded = DateTime.UtcNow };
This code works with smaller files. My C # method is called, the file is extracted, and the progressall handler is called, displaying 100%.
The problem is that I am trying to load a large file (where the progressall handler is called more than once). In this case, the handler receives calls with increasing progress rates up to 100%, as expected. But my C # method is never called, and the browser reports the following error.
POST http: // localhost: 1290 / File / AddAttachment 404 (not found)
I am confused by this error because in both cases the C # method is the same. Why is it found in one and not found in the other. I assume that the problem is that my controller method expects a complete file and that I should instead write code to handle loading in pieces. It is right? Can someone point me to the documentation on how to write a boot handler using C # / MVC?