Jon*_*ood 5 c# ajax asp.net-mvc jquery jquery-file-upload
我正在尝试在我的MVC应用程序中使用blueimp jQuery-File-Upload组件.
到目前为止,这是我的代码.
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);
});
}
});
Run Code Online (Sandbox Code Playgroud)
控制器:
[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 });
}
Run Code Online (Sandbox Code Playgroud)
此代码适用于较小的文件.调用我的C#方法,检索文件,progressall
调用处理程序显示100%.
问题是当我尝试上传一个大文件时(progressall
处理程序被多次调用).在这种情况下,按预期方式调用处理程序,将进度值增加到100%.但我的C#方法永远不会被调用,浏览器会报告以下错误.
POST http:// localhost:1290/File/AddAttachment 404(Not Found)
我对此错误感到困惑,因为C#方法在两种情况下都是相同的.为什么它在一个中找到而在另一个中找不到.我假设问题是我的控制器方法期望一个完整的文件,我必须编写代码来处理块中的上传.这是正确的吗?有人能指出我如何使用C#/ MVC编写上传处理程序的文档吗?
该死的!这是超出最大文件大小的问题。我知道这个。我以前处理过。但我对 404 错误感到困惑!噢!
归档时间: |
|
查看次数: |
1255 次 |
最近记录: |