我想通过ajax做一个ASP.NET文件上传。我在这个地方Ajax调用:ASP.NET通过ajax上传文件每次都会返回错误
$.ajax({
type: "POST",
url: '/Home/Upload',
data: formData,
dataType: 'json',
contentType: false,
processData: false,
success: function (response) {
alert('success!!');
$("#" + id).attr('disabled', false);
},
error: function (error) {
alert("errror");
}
});
,这是我的.NET代码:
[HttpPost]
public void Upload()
{
for (int i = 0; i < Request.Files.Count; i++)
{
var file = Request.Files[i];
string path = Path.Combine(Server.MapPath("~/UploadedFiles"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
}
}
当我转到该文件夹,我可以看到它被上传,但由于某种原因,AJAX回报警报错误,请帮助。
你需要用[HttpPost]一起定义了[WebMethod]属性。 – DinoMyte