gpt4 book ai didi

javascript - HttpPostedFileBase 返回 null

转载 作者:行者123 更新时间:2023-11-28 06:55:11 27 4
gpt4 key购买 nike

我无法弄清楚我做错了什么,但由于某种原因 HttpPostedFileBase 总是返回 null。当我尝试在服务器端上传文件时,我继续使用 HttpPosterFileBase 获取 null:这是我的代码:

<form method="POST" id="frmNote" action="/Client/SaveNote"  enctype="multipart/form data>
Date: @Html.TextBox("StratDate", note.StratDate)
Note: @Html.TextArea("note note.ClientNote)
<input type="file" name="file" id="file" />
<input type="submit" value="Save" class="button" />
</form>



<script type="text/javascript">
$("#frmNote").submit(function (event) {
event.preventDefault();
var formData = new FormData($('form')[0]);
formData.append("file", $('input[type=file]')[0].files[0]);
formData += "& StratDate =" + $("#StratDate").val();
formData += "& Note =" + $("# Note ").val();
$.ajax({
type: "POST",
url: "/Client/SaveNote",
data: formData,
contentType: false,
processData: false,
success: function() {
alert("note saved successfully.");
},
error: function() {
alert(result.message);
}
});
});
</script>



[HttpPost]
[ValidateInput(false)]
public JsonResult SaveNote(ActivityData note)
{

try
{
HttpPostedFileBase file = Request.Files[0];
if (file == null)
{

fileName = Path.GetFileName(file.FileName);
filePath = Path.Combine(Server.MapPath("~/image"), fileName);
file.SaveAs(filePath);

}
client.SaveNote(note);
return Json(new { error = false });
}

catch (Exception ex)
{
return Json(new { error = true, message = ex.Message });
}
}

最佳答案

使用 FormData.append 将所有参数添加到请求的帖子正文中。

formData.append("file", $('input[type=file]')[0].files[0]);
formData.append("StratDate", $("#StratDate").val());
formData.append("Note", $("#Note").val());

此外,由于您使用包含要传递的所有数据的表单调用了 FormData 构造函数,因此无需向其附加任何内容。

var formData = new FormData($('form')[0]);    
$.ajax({
type: "POST",
url: "/Client/SaveNote",
data: formData,
contentType: false,
processData: false,
success: function() {
alert("note saved successfully.");
},
error: function() {
alert(result.message);
}
});

关于javascript - HttpPostedFileBase 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32616171/

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