gpt4 book ai didi

javascript - 将 FormData 传递给 AJAX 调用时如何修复 HttpPostedFileBase null

转载 作者:行者123 更新时间:2023-12-02 23:27:44 26 4
gpt4 key购买 nike

我有一个 AJAX 调用,它将数据和文件一起传递到 ViewModel 并调用 Controller :

function fncInsTrainingLog()
{
var trainingtitle = getValOf("trainingTitle");
var ImageFile = $('#imageUploadForm')[0].files[0];

var sdata = {
TrainingTitle :trainingtitle,
ImageFile : ImageFile
}

$.ajax({
url: "/Capability/InsTrainingLog",
type: "POST",
data: JSON.stringify(sdata),
contentType: "application/json",
dataType: "json",
success: function () {
location.reload();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("A problem has been encountered. Please contact the web administrator.");
}
});
}

这是我的 Controller :

[HttpPost]
public JsonResult InsTrainingLog(TrainingModel trainingModel)
{

// Psuedo code to save file to drive
// get file from TrainingModel
// save(file)

string sp = "usp_InsTrainingLog";

object[] param =
{
"@TrainingTitle", trainingModel.TrainingTitle
};

dbHelper.ExecuteProcedureNonQuery(sp, param);
var result = param;

return Json(result, JsonRequestBehavior.AllowGet);
}

View 模型:

   public sealed class TrainingModel
{
public HttpPostedFileBase ImageFile { get; set; }
public string TrainingTitle { get; set; }

}

TrainingModel 中的 ImageFile 返回 null,但 TrainingTitle 没问题。为什么 ViewModel 无法从 AJAX 调用中读取文件?

如何将文件传递到 ViewModel 并将图像保存到我的 PC?

最佳答案

您需要使用 AJAX 调用 FormData(),将 contentType 更改为 contentType: false,并添加 processData: false

我复制了并且成功了

var trainingtitle = $("#trainingTitle").val();
var ImageFile = $('#imageUploadForm')[0].files[0];

var formData = new FormData();
formData.append('TrainingTitle', trainingtitle);
formData.append('ImageFile', ImageFile);


$.ajax({
url: "/Product/InsTrainingLog",
type: "POST",
data: formData,
contentType: false,
processData: false,
dataType: "json",
success: function () {
location.reload();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("A problem has been encountered. Please contact the web administrator.");
}
});



Updated cshtml


<div id="uploadForm">
<input type="text" name="TrainingTitle" id="trainingTitle" />
<input type="file" name="ImageFile" id="imageUploadForm" />
<button type="button" onclick="fncInsTrainingLog()">Submit</button>
</div>

function fncInsTrainingLog() {
var trainingtitle = $("#trainingTitle").val();
var ImageFile = $('#imageUploadForm')[0].files[0];

var formData = new FormData();
formData.append('TrainingTitle', trainingtitle);
formData.append('ImageFile', ImageFile);
//var sdata = {
// TrainingTitle: trainingtitle,
// ImageFile: ImageFile
//}

$.ajax({
url: "/Product/InsTrainingLog",
type: "POST",
data: formData,
contentType: false,
processData: false,
dataType: "json",
success: function () {
location.reload();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("A problem has been encountered. Please contact the web administrator.");
}
});
}

关于javascript - 将 FormData 传递给 AJAX 调用时如何修复 HttpPostedFileBase null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56657025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com