使用vue.js和asp.net web api上载文件时出现运行时异常?

使用vue.js和asp.net web api上载文件时出现运行时异常?,vue.js,asp.net-web-api,vuejs2,asp.net-web-api2,Vue.js,Asp.net Web Api,Vuejs2,Asp.net Web Api2,当我试图上传一个像这样的图像文件时,我遇到了一个例外 “没有MediaTypeFormatter可用于从媒体类型为“multipart/form data”的内容中读取“HttpFileCollection”类型的对象。” 我没有使用submit按钮,我只是想异步上传文件,而不需要任何回发 这是我的密码: HTML: <input type="button" ref="upload" name="upload" v-on:click="UploadPhoto" />

当我试图上传一个像这样的图像文件时,我遇到了一个例外

“没有MediaTypeFormatter可用于从媒体类型为“multipart/form data”的内容中读取“HttpFileCollection”类型的对象。”

我没有使用submit按钮,我只是想异步上传文件,而不需要任何回发

这是我的密码:

HTML:

 <input type="button" ref="upload" name="upload" v-on:click="UploadPhoto" />
             async UploadPhoto() {
                 let image = new FormData();
                image.append('name', 'fileUploader');
                image.append('file', this.$refs.fileUploader.files[0]);

                let config = {
                    headers: {
                      //  'Content-Type': 'image/*'
                       'Content-Type': 'multipart/form-data'
                    }
                };

                // call server upload request here...
                let testRes = await axios.post('http://localhost:44347/api/GetUser/UploadPhoto', image, config).then()
                    .catch(error => console.log(error));
                 this.result = testRes;
                 console.log(image);
            },
[HttpPost]
[Route("api/GetUser/UploadPhoto")]
public async Task<IHttpActionResult> UploadPhoto([FromBody] HttpFileCollection selectedfiles)
{
 await Task.Run(() =>
            {
   string sPath= HostingEnvironment.MapPath("~/Dummy/");
             //   HttpFileCollection selectedfiles = HttpContext.Current.Request.Files;
                 selectedfiles = HttpContext.Current.Request.Files;
                HttpPostedFile file = selectedfiles[0];
                string fileName = new FileInfo(file.FileName).Name;

                if (file.ContentLength > 0)
                {
                    Guid guid = new Guid();
                    string modFilename = guid.ToString() + "_" + fileName;
                    file.SaveAs(sPath + Path.GetFileName(modFilename));
                }
             });

            return Ok();
}
Web Api:

 <input type="button" ref="upload" name="upload" v-on:click="UploadPhoto" />
             async UploadPhoto() {
                 let image = new FormData();
                image.append('name', 'fileUploader');
                image.append('file', this.$refs.fileUploader.files[0]);

                let config = {
                    headers: {
                      //  'Content-Type': 'image/*'
                       'Content-Type': 'multipart/form-data'
                    }
                };

                // call server upload request here...
                let testRes = await axios.post('http://localhost:44347/api/GetUser/UploadPhoto', image, config).then()
                    .catch(error => console.log(error));
                 this.result = testRes;
                 console.log(image);
            },
[HttpPost]
[Route("api/GetUser/UploadPhoto")]
public async Task<IHttpActionResult> UploadPhoto([FromBody] HttpFileCollection selectedfiles)
{
 await Task.Run(() =>
            {
   string sPath= HostingEnvironment.MapPath("~/Dummy/");
             //   HttpFileCollection selectedfiles = HttpContext.Current.Request.Files;
                 selectedfiles = HttpContext.Current.Request.Files;
                HttpPostedFile file = selectedfiles[0];
                string fileName = new FileInfo(file.FileName).Name;

                if (file.ContentLength > 0)
                {
                    Guid guid = new Guid();
                    string modFilename = guid.ToString() + "_" + fileName;
                    file.SaveAs(sPath + Path.GetFileName(modFilename));
                }
             });

            return Ok();
}
[HttpPost]
[路由(“api/GetUser/UploadPhoto”)]
公共异步任务上载照片([FromBody]HttpFileCollection selectedfiles)
{
等待任务。运行(()=>
{
字符串sPath=HostingEnvironment.MapPath(“~/Dummy/”);
//HttpFileCollection selectedfiles=HttpContext.Current.Request.Files;
selectedfiles=HttpContext.Current.Request.Files;
HttpPostedFile file=selectedfiles[0];
字符串文件名=新文件信息(file.fileName).Name;
如果(file.ContentLength>0)
{
Guid=新Guid();
string modFilename=guid.ToString()+“_”+文件名;
SaveAs(sPath+Path.GetFileName(modFilename));
}
});
返回Ok();
}

HttpFileCollection似乎有问题。我不确定您使用的asp.net版本,因为您没有指定,但我建议尝试:

public异步任务上传照片(ifformfile)
如果要进行多个文件上载,只需使用:

List<IFormFile> yourKeyNameFromFormData
从FormData中列出您的KeyName

谢谢您的建议,但我不使用.net core。如果我仍然使用较低版本的asp.net web api,它还能工作吗?我需要在模板上使用表单提交吗?