file-upload - 如何将表单数据(即带有表单数据的文件)从 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, …}
解决方案
推荐阅读
- react-native - 使用值从数组中删除特定项
- android - 在 android 中使用 dagger2 时缺少 DaggerAppComponent
- c - 从字符串输入中查找最长的单词 + 长度
- java - 如何为 ResponseEntity<> 返回“整数”类型并在 api 页面上获取结果?
- php - PHP将记录ID传递到另一个页面而不在URL中显示它
- python - Python中的文件处理代码没有给出任何输出
- java - 如何在while循环迭代中将所有行放在HashMap中
- python - Django 在错误的应用程序中搜索 URL
- reactjs - 让 Next HOC 连接到 Redux 商店
- java - Spring DATA JPA:如何在保存嵌套实体的数据库约束(违反唯一键)时获得成功/失败