上传图片后台
public void UploadFile()
{
if (HttpContext.Current.Request.Files.AllKeys.Any())
{
// Get the uploaded image from the Files collectionvar
var httpPostedFile = HttpContext.Current.Request.Files["UploadedImage"];
if (httpPostedFile != null)
{
// Validate the uploaded image(optional)// Get the complete file path
var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"),httpPostedFile.FileName );
// Save the uploaded file to"UploadedFiles" folder
httpPostedFile.SaveAs(fileSavePath);
filepath = Path.Combine("~/UploadedFiles", httpPostedFile.FileName);
}
}
}
前台
function Uploag() {
//上传文件
var fdate = new FormData();
var file = $("#fileUpload").get(0).files;
//如果文件存在
if (file != null) {
//自定义一个文件名称并添加到尾部
fdate.append("UploadedImage", file[0]);
}
var ajaxRequest = $.ajax({
url: "http://localhost:54489/api/Goods/UploadFile",
type: "post",
processData: false,
contentType: false,
data: fdate
});
}
设置form 的 contentType=multiple/form-data