为 jquery 文件上传编写服务器端处理程序
Writing server-side handler for jquery file upload
我正在尝试让 blueimp jQuery-File-Upload 组件在我的 MVC 应用程序中工作。
到目前为止,这是我的代码。
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);
});
}
});
控制器:
[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 });
}
此代码适用于较小的文件。我的 C# 方法被调用,文件被检索,progressall
处理程序被调用显示 100%。
问题是当我尝试上传大文件时(progressall
处理程序被调用不止一次)。在这种情况下,处理程序将按预期以最高 100% 的递增进度值被调用。但我的 C# 方法从未被调用,浏览器报告以下错误。
POST http://localhost:1290/File/AddAttachment 404 (Not Found)
我对这个错误感到困惑,因为 C# 方法在这两种情况下是相同的。为什么它在一个中找到而在另一个中找不到。我假设问题是我的控制器方法需要一个完整的文件,我必须改为编写代码来处理分块上传。这是正确的吗?谁能指出有关如何使用 C#/MVC 编写上传处理程序的文档?
该死!这是超出最大文件大小的问题。我知道这个。我以前处理过。但是我对 404 错误感到困惑!哦!
其实是有一个触发器,依赖于未在其基础上下载的high文件的错误打开。
404错误取决于IIS安全协议
如果你要检查Request和Response的细节,你可以自己看
不建议IIS管理员解决
如果您将在应用程序级别设置它
web.config中的示例;
<System.webServer>
<Security>
<RequestFiltering>
<requestLimits maxAllowedContentLength = "30000000" />
</ requestFiltering>
</ security>
</system.webServer>
我正在尝试让 blueimp jQuery-File-Upload 组件在我的 MVC 应用程序中工作。
到目前为止,这是我的代码。
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);
});
}
});
控制器:
[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 });
}
此代码适用于较小的文件。我的 C# 方法被调用,文件被检索,progressall
处理程序被调用显示 100%。
问题是当我尝试上传大文件时(progressall
处理程序被调用不止一次)。在这种情况下,处理程序将按预期以最高 100% 的递增进度值被调用。但我的 C# 方法从未被调用,浏览器报告以下错误。
POST http://localhost:1290/File/AddAttachment 404 (Not Found)
我对这个错误感到困惑,因为 C# 方法在这两种情况下是相同的。为什么它在一个中找到而在另一个中找不到。我假设问题是我的控制器方法需要一个完整的文件,我必须改为编写代码来处理分块上传。这是正确的吗?谁能指出有关如何使用 C#/MVC 编写上传处理程序的文档?
该死!这是超出最大文件大小的问题。我知道这个。我以前处理过。但是我对 404 错误感到困惑!哦!
其实是有一个触发器,依赖于未在其基础上下载的high文件的错误打开。
404错误取决于IIS安全协议
如果你要检查Request和Response的细节,你可以自己看
不建议IIS管理员解决
如果您将在应用程序级别设置它
web.config中的示例;
<System.webServer>
<Security>
<RequestFiltering>
<requestLimits maxAllowedContentLength = "30000000" />
</ requestFiltering>
</ security>
</system.webServer>