【问题标题】:How to pass file name with Angular HttpRequest while uploading the file上传文件时如何使用Angular HttpRequest传递文件名
【发布时间】:2020-07-17 14:57:21
【问题描述】:

Angular 服务使用 Angular 的 HttpRequest 上传文件:

  upload(file: File) {
    const req = new HttpRequest(
      'POST', 
      this.SERVER_URL, 
      file,
      { reportProgress: true }
      );
    return this.http.request(req)
  }

HTTP 服务器(运行 Flask)获取这个 POST 请求,我可以使用 request.data 访问文件数据。不幸的是,没有提供文件名。如何向 Angular 的 HttpRequest req 提供要与文件数据一起传递的文件名(可能还有其他自定义数据)?

【问题讨论】:

    标签: angular typescript http request httprequest


    【解决方案1】:

    要访问文件名,您可以这样做

        upload(file: File) {
        const fileName = file.target.files[0].name;
        const req = new HttpRequest(
          'POST', 
          this.SERVER_URL, 
          fileName,
          { reportProgress: true }
          );
        return this.http.request(req)
      }
    

    【讨论】:

    • 但是您的方法不会上传文件。我们将只传递文件名。如何上传文件并提交同名HttpRequest的文件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 2020-01-24
    • 2017-11-13
    • 2012-11-09
    • 1970-01-01
    相关资源
    最近更新 更多