【问题标题】:Difference between form data and base64 for sending image to .net API将图像发送到 .net API 的表单数据和 base64 之间的区别
【发布时间】:2017-08-30 18:37:26
【问题描述】:

我正在开发一个 Angular 2 项目,我想将带有数据的图像发送到 .NET Core API。我怎样才能做到这一点? 我正在使用其输出为 base64 的裁剪器。在其他请求中,我只是将图像作为 formdata 发送,但现在我必须发送一些图像和元数据。 我想知道formdata和base64发送有什么区别,哪个更好?

【问题讨论】:

  • 你能分享一下你到目前为止的尝试吗?
  • 当我发送 base64 时,API 方法参数为空,然后我用此代码替换文件 { item.imageFile = result.croppedPicture.replace(/^data:image\/[a-z]+;base64, /, ""); }。现在我的方法将元数据与图像作为字节数组
  • 请更新您的问题并添加您迄今为止尝试过的代码。如果可能的话,做一个 Plunker。

标签: angular metadata asp.net-core-webapi


【解决方案1】:

您的服务器端点应如下所示:

[HttpPost("upload")]
public void Upload(List<IFormFile> files, string example) {}

您的服务功能应如下所示:

let formData = new FormData();
formData.append('files', file);
formData.append('example', 'extra');

return this.http.post(this.url + '/upload', formData);

文件直接来自输入 type="file" (inputEvent.target.files[0])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2012-01-21
    • 2019-09-15
    • 2020-07-20
    • 2016-02-03
    • 2011-07-12
    • 2019-11-06
    相关资源
    最近更新 更多