gpt4 book ai didi

jquery - 用ajax上传图片,HttpPostedFileBase为null Mvc Asp

转载 作者:行者123 更新时间:2023-12-03 22:41:31 26 4
gpt4 key购买 nike

我必须在提交表单之前上传图片。所以我用ajax来做。

这是我的帮助 Controller :

[HttpPost]
public void AcceptUpload(HttpPostedFileBase TemporaryForLast, string ReferanceNo)
{
TemporaryForLast.SaveAs(Server.MapPath("~/Profiles/images/" + ReferanceNo + "/") + "HoldCopy" + ".jpg");
}

这是我的观点:

<input id="HoldCopy" type="file" name="HoldCopy" accept="image/*">

Ans 脚本:

$("#acceptUpload").click(function () {
var formData= new FormData();
var imagefile=document.getElementById("HoldCopy").files[0];
formData.append("imageFile",imageFile);
var xhr = new XMLHttpRequest();
xhr.open("POST", "/Help/AcceptUpload", true);
xhr.addEventListener("load", function (evt) { UploadComplete(evt); }, false);
xhr.addEventListener("error", function (evt) { UploadFailed(evt); }, false);
xhr.send(formData);
});

error功能也更新了。

最佳答案

查看

您可以使用 Jquery Ajax 代替

<script>
function SubmitButtonOnclick()
{
var formData= new FormData();
var imagefile=document.getElementById("imageFile").files[0];
formData.append("imageFile",imageFile);
var xhr = new XMLHttpRequest();
xhr.open("POST", "/Help/AcceptUpload", true);
xhr.addEventListener("load", function (evt) { UploadComplete(evt); }, false);
xhr.addEventListener("error", function (evt) { UploadFailed(evt); }, false);
xhr.send(formData);

}

function UploadComplete(evt) {
if (evt.target.status == 200)
alert("Logo uploaded successfully.");

else
alert("Error Uploading File");
}

function UploadFailed(evt) {
alert("There was an error attempting to upload the file.");

}
</script>

关于jquery - 用ajax上传图片,HttpPostedFileBase为null Mvc Asp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23748538/

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