//上传图片
$("#file").change(function () {
var getImgData = new FormData($("#FormData")[0]);
$.ajax({
url: "/WeekTest3View/UploadingFile",
contentType: false,
processData: false,
cache: false,
type: 'POST',
data: getImgData,
success: function (data) {
$("#DisplayImage").attr("src", data);
$("input[name=FileName]").val(data);
}
});
});
View控制器:
/// <summary>
/// 上传图片
/// </summary>
/// <returns></returns>
[HttpPost]
public string UploadingFile()
{
HttpPostedFileBase getFile = Request.Files["file"];
if (getFile != null)
{
string getPath = Server.MapPath("~/Image/");
if (!Directory.Exists(getPath))
{
Directory.CreateDirectory(getPath);
}
string newPath = Path.Combine(getPath, getFile.FileName);
getFile.SaveAs(newPath);
return "/Image/" + getFile.FileName;
}
else
{
return null;
}
}