Angular:使用Angular HTTP表单数据发送远程文件

Angular:使用Angular HTTP表单数据发送远程文件,angular,file,http,multipartform-data,Angular,File,Http,Multipartform Data,我的服务器端点上有一个文件可用http://xyz/file 我想使用多部分/表单数据通过HTTP POST以Angular 6格式发送此文件。感谢,这是我的方法: const filePath = 'http://xyz/file' const formData = new FormData(); formData.append('file', filePath); this.http.post(endpoint, formData).subscribe(re

我的服务器端点上有一个文件可用
http://xyz/file

我想使用多部分/表单数据通过HTTP POST以Angular 6格式发送此文件。感谢,这是我的方法:

    const filePath = 'http://xyz/file'
    const formData = new FormData();
    formData.append('file', filePath);

    this.http.post(endpoint, formData).subscribe(response => {
        console.log(response);
    });
问题是,如果我指定指向文件端点的文件路径而不是文件本身,这似乎不起作用。尝试此操作时,控制台中出现错误
error-TypeError:“FormData.append的参数2不是对象。”


我怎样才能解决这个问题?我能用远程文件发帖子吗?或者我需要先下载文件,然后再发送吗?我该怎么做?

您不能只发送指向文件的链接。Formdata字段应包含文件内容。您可以分两步完成此操作

  • 创建一个将远程文件下载到Blob的函数
  • 通过Post请求发送blob

  • 您不能只发送指向文件的链接。Formdata字段应包含文件内容。您可以分两步完成此操作

  • 创建一个将远程文件下载到Blob的函数
  • 通过Post请求发送blob
  • fetch('https://some/file/here.png')
      .then(res => res.blob()) // Gets the response and returns it as a blob
      .then(blob => {
        var formData = new FormData();
        formData.append("file", blob);
    
        this.http.post(endpoint, formData).subscribe(response => {
            console.log(response);
        });
    
      });