欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

图片上传

程序员文章站 2022-12-11 07:58:56
//上传图片 $("#file").change(function () { var getImgData = new FormData($("#FormData")[0]); $.ajax({ url: "/WeekTest3View/UploadingFile", contentType: fa ......

//上传图片
$("#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;
}
}