C# MVC AJAX文件上载-停止工作
我有一个通过AJAX请求将文件和其他数据上传到MVC控制器的解决方案 很长一段时间以来,它一直运行良好,但今天,我一直在调查人们报告的问题,现在它似乎根本无法运行;我现在收到返回的“未找到资源”错误 如果我从AJAX调用中删除“data:fileData”,那么它就能够解析URL,但是显然,没有数据要处理 有人能解释为什么它会以这种方式停止工作吗?我无法解释这一点 简言之: AJAX调用不再能够解析控制器方法URL。在存储库历史记录中,最近没有对此功能进行明显的更改 谢谢,, 马修 更新 对于那些感兴趣的人来说,事实证明,我要测试的特定PDF文件的大小一直是罪魁祸首……这个解决方案仍然有效,并且可以在 将以下内容添加到web.config文件:C# MVC AJAX文件上载-停止工作,c#,jquery,ajax,asp.net-mvc,C#,Jquery,Ajax,Asp.net Mvc,我有一个通过AJAX请求将文件和其他数据上传到MVC控制器的解决方案 很长一段时间以来,它一直运行良好,但今天,我一直在调查人们报告的问题,现在它似乎根本无法运行;我现在收到返回的“未找到资源”错误 如果我从AJAX调用中删除“data:fileData”,那么它就能够解析URL,但是显然,没有数据要处理 有人能解释为什么它会以这种方式停止工作吗?我无法解释这一点 简言之: AJAX调用不再能够解析控制器方法URL。在存储库历史记录中,最近没有对此功能进行明显的更改 谢谢,, 马修 更新 对于那
错误来自您的C代码。我建议仔细研究一下,找出到底是哪一部分逻辑导致了回复@Rory的问题。问题是,C#代码甚至没有被访问(除非我从AJAX调用中删除数据),所以我没有什么可以插手的。当你说你得到“资源未找到”时,状态代码是404吗?我实际上没有看到状态代码,但我使用Firebug得到的响应是:资源未找到{“appName”:“Chrome”,“requestId:“b05fab316df7456b97540fb876a093c8”}您可以尝试以下内容类型:contentType:“多部分/表单数据”
<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>
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);
}