我必须在提交表单之前上传图片。所以我用ajax来做它。
这是我的HelpController:
[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 Script:
$("#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
功能也会更新。
答案 0 :(得分:3)
查看
您可以使用
代替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>