Server-side handler entry for loading jquery file

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 }; // Load content fileAttachment.Content = new byte[file.ContentLength]; file.InputStream.Read(fileAttachment.Content, 0, file.ContentLength); // Add to database dbContext.FileAttachments.Add(fileAttachment); // Add to results files.Add(new FileAttachmentFileModel { id = fileAttachment.Id.ToString(), name = file.FileName, size = file.ContentLength.FormatAsFileSize(), //action = FileAttachmentFileModel.AddAction }); } } dbContext.SaveChanges(); transaction.Commit(); } } } catch (Exception ex) { // TODO: return Json(new { message = ex.Message }); } return Json(new { files = files }); } 

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?

+5
source share
2 answers

Come on! This was a problem with exceeding the maximum file size. I know it. I communicated with this before. But I was embarrassed by the 404 error! D'o!

+3
source

In fact, there is a trigger that depends on the incorrect opening of a tall file that is not loaded based on it.

Error 404 Depends on IIS Security Protocol

If you are going to study the details of the request and response, you can see it yourself

I do not recommend allowing it by the IIS administrator.

If you install it at the application level

example in web.config;

 <System.webServer> <Security> <RequestFiltering> <requestLimits maxAllowedContentLength = "30000000" /> </ requestFiltering> </ security> </system.webServer> 
+1
source

Source: https://habr.com/ru/post/1273189/


All Articles