首页 新闻 会员 周边

mvc调用webapi上传图片

0
悬赏园豆:5 [已解决问题] 解决于 2020-10-07 19:52

上传图片后台
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
    });
}
无可wuke的主页 无可wuke | 初学一级 | 园豆:197
提问于:2020-08-29 11:39
< >
分享
最佳答案
0

设置form 的 contentType=multiple/form-data

收获园豆:5
flyfishing | 小虾三级 |园豆:943 | 2020-08-29 16:48
其他回答(1)
0

如何用.net mvc和webapi实现文件上传
https://www.52interview.com/solutions/27

智客工坊 | 园豆:1855 (小虾三级) | 2020-08-30 12:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册