首页 > 解决方案 > 如何将表单数据(即带有表单数据的文件)从 Angular 5 发送到 Web API

问题描述

我想将表单数据(文件上传)沿着不同的其他参数从 Angular 发送到 C# webAPI。

网络接口:

[Route("PostTime")]
    [HttpPost]
    public async Task<IHttpActionResult> PostTime(PostTimeObject _Model)
    {
        try
        {

            return Content(System.Net.HttpStatusCode.NotModified, "Unable to read values");
        }
        catch (Exception)
        {
            return Content(System.Net.HttpStatusCode.NoContent , "Unable to read values");
        }
    }

PostTime 是类:

    public class PostTimeObject
{
public string ClientID { get; set; }
        public string ClientName { get; set; 
}

角度:

let selectedFile =<File> evt.target.files[0];
this.fdata.append('attachedFile',selectedFile,selectedFile.name)// fdata is FormData

P_Data.clientID : this.Form.control["Clientid"].value
P_Data.ClientName : this.Form.control["ClientName "].value

this.http.post("localhost/Times/PostTime",P_Data,{ headers: new HttpHeaders({ "Content-Type": "multipart/form-data" })}).subscribe((s)=>
    {
      console.log(s)
    })

在浏览器中接收错误:

HttpErrorResponse {headers: HttpHeaders, status: 415, statusText: "Unsupported Media Type", url: "http://localhost:53265/Timesheet/PostTimeSheet", ok: false, …}

标签: file-uploadparametersangular6

解决方案


推荐阅读