我想通过打字稿发布文件上传图像。因此,我需要一个具有两个Form-Data-Values的帖子:
因此,我做了这样的事情: (CLient,Typescript,Angular)
public uploadImage(image: Blob, category: string): Observable<any> {
const body = new FormData();
body.append('file', image, 'bla.jpg');
body.append('category', category);
return this.http.post(this.actionUrl, body);
}
仅供参考,服务器端如下所示: (服务器,c#,ASp .Net Core)
[HttpPost]
public async Task Post(IFormFile file, string category)
{
var uploads = Path.Combine(@"C:\Users\Dumann\Pictures\uploads");
if (file.Length > 0)
{
using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName + category), FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
}
}
运行此命令时,服务器会接受类别,但文件仍为 NULL 。我的错误在哪里,如何解决?
提前谢谢大家!