我有一个解决方案,可以通过AJAX请求将文件和其他数据上传到MVC控制器。
很长一段时间,这种情况一直很好,但是今天,我一直在调查人们用它报道的问题,现在它似乎根本没有运作;我现在得到的资源没有找到"错误返回。
如果我删除" data:fileData,"从AJAX调用,然后它能够解析URL,但显然,没有数据可以处理。
有人能够阐明为什么这会以这种方式停止运作吗?我无法解释这一点。
AJAX调用不再能够解析控制器方法URL。最近没有对存储库历史记录中的此功能进行明显更改。
谢谢你, 马修。
为了那些感兴趣的人,事实证明我给予测试的特定PDF文件的大小一直是罪魁祸首...这个解决方案仍然有效,并且可以通过
工作来处理更大的文件<system.web>
<httpRuntime maxRequestLength="10240" />
<!--This allows larger files to be uploaded (10MB in KB)-->
</system.web>
...
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="10485760" />
<!--This allows larger files to be uploaded (10MB in Bytes)-->
</requestFiltering>
</security>
</system.webServer>
页内Javascript:
function SaveAttachmentData() {
var attachmentID = $('#attachmentrecordid').val();
var servID = $('#servrecordid').val();
var description = $('#txtattachmentdescription').val();
// Checking whether FormData is available in browser
if (window.FormData !== undefined) {
var fileUpload = $("#attachmentfile").get(0);
var files = fileUpload.files;
// Create FormData object
var fileData = new FormData();
// Looping over all files and add it to FormData object
for (var i = 0; i < files.length; i++) {
fileData.append(files[i].name, files[i]);
}
// Adding keys to FormData object
fileData.append('service_id', servID);
fileData.append('dataid', attachmentID);
fileData.append('description', description);
var validData = true;
if (!isNormalInteger(servID)){validData = false;}
if (servID < 1) {validData = false;}
if (files.length == 0 && $('#attachmentrecordid').val() < 1){validData = false;}
if (validData){ //All data is valid
$.ajax({
url: '/Service/AttachmentCreate',
type: "POST",
contentType: false, // Not to set any content header
processData: false, // Not to process data
data: fileData,
success: function (result) {
ReloadAttachmentTable();
},
error: function (err) {
ReloadAttachmentTable();
}
});
}
else{
//Data is not valid
alert('Invalid data entered!');
//ShowBannerMessageWarning('Warning!','Invalid data entered!');
}
} else {
alert("FormData is not supported.");
}
}
控制器:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AttachmentCreate()
{
//Upload file
string message = "";
DataModel dm = new DataModel();
Service.Attachment sa = new Service.Attachment();
//Get request data
string description = Request.Form["description"];
int servid = int.Parse(Request.Form["service_id"]);
HttpFileCollectionBase files = Request.Files;
//Only continue if a file was uploaded
if (files.Count > 0)
{
//Get the file
HttpPostedFileBase file = files[0];
string fname;
// Checking for Internet Explorer
if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
{
string[] testfiles = file.FileName.Split(new char[] { '\\' });
fname = testfiles[testfiles.Length - 1];
}
else
{
fname = file.FileName;
}
//Update attachment properties
sa.ID = servid;
sa.Description = description;
sa.Filename = fname; //Assign fname before the full path is added to it
//Get full filename from attachment class property
fname = sa.Filename_Structured;
// Get the complete folder path and store the file inside it.
fname = Path.Combine(ConfigurationManager.AppSettings["AttachmentFolderLocation"].ToString(), fname);
//Save file to server
file.SaveAs(fname);
//Create file attachment record against service
message = dm.CreateAttachmentData(ref sa);
message = message == "" ? "Success" : message;
}
else { message = "No file uploaded"; }
return Content(message);
}