我的文件上传停止工作
my file upload stopped working
我正在上传文件,我的上传代码是正确的。
这个例子几乎就是我正在做的:
但是,我的项目出了点问题,它突然停止工作了。我一直在努力解决这个问题,但无济于事。我最近转换为使用此功能提交表单的单页应用程序:
var ajaxFormSubmit = function (contentDiv, formDiv, modalId, formId, controllerPath) {
$(".loader").show();
$("#".concat(formId)).on("submit", function (e) {
console.log("ajax form submitted");
e.preventDefault(); // prevent standard form submission
var form = $(this);
$.ajax({
url: form.attr("action"),
method: form.attr("method"), // post
data: form.serialize(),
error: function () {
$(".loader").hide();
alert("An error occurred.");
},
success: function (partialResult) {
console.log(partialResult.length);
if (partialResult.length === 0) {
console.log("form archhived");
$("#".concat(modalId)).modal('hide');
//forcing the backdrop to go away, something is wrong with the modal, it needs work.
$('.modal-backdrop').remove();
getManager(controllerPath, contentDiv);
//get gunnery manager
}
else {
console.log("form came back");
$("#".concat(formDiv)).html(partialResult);
$(".loader").hide();
}
}
});
});
}
型号:
public class person
{
public int id {get;set;}
public string fName {get;set;}
public string lName {get;set;}
public HttpPostedFileBase attachment {get;set;}
}
这是我的控制器部分:
if (model.attachment != null)
{
var file = model.attachment;
if (file.ContentLength > 0)
{
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"),Path.GetFileName(file.FileName));
file.SaveAs(path);
System.Diagnostics.Debug.WriteLine(path);
model.attachmentLink = path;
}
}
我发现这一次我的文件上传停止工作并非巧合,但我现在才注意到。如何使我的文件上传工作?
注意
该视图使用 html 助手非常标准。
尝试使用 FormData
var ajaxFormSubmit = function (contentDiv, formDiv, modalId, formId, controllerPath) {
$(".loader").show();
$("#".concat(formId)).on("submit", function (e) {
console.log("ajax form submitted");
e.preventDefault(); // prevent standard form submission
var form = $(this);
var formData = new FormData(); // CREATE FORM DATA OBJECT
var fileUpload = $("#file").get(0); // your file element
var files = fileUpload.files;
formData.append("YOR_FILE_DATA_KEY_NAME", files[0]);
// add all form elements like following
// formData.append("key",value);
formData.append("id",$("#id").val()); // check the selector if it is correct
formData.append("fName",$("#fName").val());
formData.append("lName",$("#lName").val());
$.ajax({
url: form.attr("action"),
method: "POST", // post
contentType: false, // Not to set any content header
processData: false, // Not to process data
data: formData, // pass this form data instead of form.serialize()
error: function () {
$(".loader").hide();
alert("An error occurred.");
},
success: function (partialResult) {
console.log(partialResult.length);
if (partialResult.length === 0) {
console.log("form archhived");
$("#".concat(modalId)).modal('hide');
//forcing the backdrop to go away, something is wrong with the modal, it needs work.
$('.modal-backdrop').remove();
getManager(controllerPath, contentDiv);
//get gunnery manager
}
else {
console.log("form came back");
$("#".concat(formDiv)).html(partialResult);
$(".loader").hide();
}
}
});
});
}
编辑:将 form.seralize() 转换为 FormData 对象的解决方案
function ConvertToFormData(serializedArray, fileInputID)
{
var formData = new FormData();
//var serializedArray = $("form").serializeArray();
for(var i = 0; i < serializedArray.length;i++)
{
if(serializedArray[i].name != "FILE_INPUT_ELEMENT") // don't add file input here
formData.append(serializedArray[i].name,serializedArray[i].value);
}
var fileUpload = $(fileInputID).get(0); // your file element
var files = fileUpload.files;
formData.append(fileInputID, files[0]);
return formData;
}
这个功能怎么用???
var data = ConvertToFormData($("form").serializeArray(),"Your_file_input_id");
我正在上传文件,我的上传代码是正确的。
这个例子几乎就是我正在做的:
但是,我的项目出了点问题,它突然停止工作了。我一直在努力解决这个问题,但无济于事。我最近转换为使用此功能提交表单的单页应用程序:
var ajaxFormSubmit = function (contentDiv, formDiv, modalId, formId, controllerPath) {
$(".loader").show();
$("#".concat(formId)).on("submit", function (e) {
console.log("ajax form submitted");
e.preventDefault(); // prevent standard form submission
var form = $(this);
$.ajax({
url: form.attr("action"),
method: form.attr("method"), // post
data: form.serialize(),
error: function () {
$(".loader").hide();
alert("An error occurred.");
},
success: function (partialResult) {
console.log(partialResult.length);
if (partialResult.length === 0) {
console.log("form archhived");
$("#".concat(modalId)).modal('hide');
//forcing the backdrop to go away, something is wrong with the modal, it needs work.
$('.modal-backdrop').remove();
getManager(controllerPath, contentDiv);
//get gunnery manager
}
else {
console.log("form came back");
$("#".concat(formDiv)).html(partialResult);
$(".loader").hide();
}
}
});
});
}
型号:
public class person
{
public int id {get;set;}
public string fName {get;set;}
public string lName {get;set;}
public HttpPostedFileBase attachment {get;set;}
}
这是我的控制器部分:
if (model.attachment != null)
{
var file = model.attachment;
if (file.ContentLength > 0)
{
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"),Path.GetFileName(file.FileName));
file.SaveAs(path);
System.Diagnostics.Debug.WriteLine(path);
model.attachmentLink = path;
}
}
我发现这一次我的文件上传停止工作并非巧合,但我现在才注意到。如何使我的文件上传工作?
注意 该视图使用 html 助手非常标准。
尝试使用 FormData
var ajaxFormSubmit = function (contentDiv, formDiv, modalId, formId, controllerPath) {
$(".loader").show();
$("#".concat(formId)).on("submit", function (e) {
console.log("ajax form submitted");
e.preventDefault(); // prevent standard form submission
var form = $(this);
var formData = new FormData(); // CREATE FORM DATA OBJECT
var fileUpload = $("#file").get(0); // your file element
var files = fileUpload.files;
formData.append("YOR_FILE_DATA_KEY_NAME", files[0]);
// add all form elements like following
// formData.append("key",value);
formData.append("id",$("#id").val()); // check the selector if it is correct
formData.append("fName",$("#fName").val());
formData.append("lName",$("#lName").val());
$.ajax({
url: form.attr("action"),
method: "POST", // post
contentType: false, // Not to set any content header
processData: false, // Not to process data
data: formData, // pass this form data instead of form.serialize()
error: function () {
$(".loader").hide();
alert("An error occurred.");
},
success: function (partialResult) {
console.log(partialResult.length);
if (partialResult.length === 0) {
console.log("form archhived");
$("#".concat(modalId)).modal('hide');
//forcing the backdrop to go away, something is wrong with the modal, it needs work.
$('.modal-backdrop').remove();
getManager(controllerPath, contentDiv);
//get gunnery manager
}
else {
console.log("form came back");
$("#".concat(formDiv)).html(partialResult);
$(".loader").hide();
}
}
});
});
}
编辑:将 form.seralize() 转换为 FormData 对象的解决方案
function ConvertToFormData(serializedArray, fileInputID)
{
var formData = new FormData();
//var serializedArray = $("form").serializeArray();
for(var i = 0; i < serializedArray.length;i++)
{
if(serializedArray[i].name != "FILE_INPUT_ELEMENT") // don't add file input here
formData.append(serializedArray[i].name,serializedArray[i].value);
}
var fileUpload = $(fileInputID).get(0); // your file element
var files = fileUpload.files;
formData.append(fileInputID, files[0]);
return formData;
}
这个功能怎么用???
var data = ConvertToFormData($("form").serializeArray(),"Your_file_input_id");