【问题标题】:Http post a file dataHttp post一个文件数据
【发布时间】:2019-02-26 12:18:05
【问题描述】:

我的客户端上有两个<input> 标签来选择一个文件。一旦客户端选择了文件(24 位 BMP 640*480),我必须制作一个 http.post 以便我可以保存每个文件的 imageData 并在需要时制作一个 http.get。我尝试发布一个 ImageData 对象或只是一个 Uint8ClampedArray 但我遇到了一些错误。现在我尝试将它转换为 base64 并发送它,但我仍然没有得到任何东西。

这是我的 http.post:

public submitInfo(): void {
  this.http.post("http://localhost:3000/sologame", { "name": this.game.gameName, "image1": this.game.picture }, HTTP_OPTIONS).pipe(
    catchError(this.handleError("submitInfo"))).subscribe();
}

这是我现在尝试发送 base64 字符串时遇到的错误:

如何发送我的图像数据?

【问题讨论】:

标签: html angular typescript http input


【解决方案1】:

有两种方法:

  1. 发送二进制数据

onUpload(selectedFile: File) {
    this.http.post('api/file-upload', selectedFile).subscribe(...);
}
  1. 作为表单数据发送

onUpload(selectedFile: File) {
    const uploadData = new FormData();
    uploadData.append('file', selectedFile, selectedFile.name);
    this.http.post('api/file-upload', uploadData).subscribe(...);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多