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