问题描述
- MVC Ajax.Begin()异步上传不了,求解救
-
View代码:@using (Ajax.BeginForm("ProcessImagUpload", "Async", new AjaxOptions() { HttpMethod = "post", OnSuccess = "afterUpload" }, new { enctype = "multipart/form-data" })) { <input type="file" name="imgFile" /> <input type="submit" value="异步上传图片" /> }
Control代码
public ActionResult ProcessImagUpload()
{
var file = Request.Files["imgFile"];string path ="/Upload/"+Guid.NewGuid().ToString()+ file.FileName; file.SaveAs(Request.MapPath(path)); return Content(path); }
查看请求报文
请求 URL:http://localhost:14092/Async/ProcessImagUpload?Length=5
请求方式:POST
状态代码:500 Internal Server Error
请求 HTTP 报头查看源代码
Accept:*/*
Accept-Encoding:gzip,deflate
Accept-Language:zh-CN
Connection:keep-alive
Content-Length:31
Content-Type:application/x-www-form-urlencoded; charset=UTF-8大半夜的求大神拯救
解决方案
string path ="/Upload/"+Guid.NewGuid().ToString()+ file.FileName;
->
string path ="~/Upload/"+Guid.NewGuid().ToString()+ file.FileName;
看看
解决方案二:
你的文件夹应该不存在吧,
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
或是
string path ="/Upload/"+Guid.NewGuid().ToString()+“/”+ file.FileName;?