Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想使用angular 6上传文件,并将其与API连接,但它采用默认类型;application/json“;,我想把它改成文件格式_Angular_Angular Services - Fatal编程技术网

我想使用angular 6上传文件,并将其与API连接,但它采用默认类型;application/json“;,我想把它改成文件格式

我想使用angular 6上传文件,并将其与API连接,但它采用默认类型;application/json“;,我想把它改成文件格式,angular,angular-services,Angular,Angular Services,我是这样做的: fileUpload(data){ let headers = new HttpHeaders().set('file', data); headers.append('Content-Type', 'application/file'); let file_upload = { headers: headers, }; console.log(data, "file upload") r

我是这样做的:

fileUpload(data){

      let headers = new HttpHeaders().set('file', data);
         headers.append('Content-Type', 'application/file');
      let file_upload =  {
        headers: headers,
      };

    console.log(data, "file upload")
    return this.httpClient.post('api/data_loader/file/', file_upload);
  }
错误

请求中不支持的媒体类型“application/json”

预期结果:我想将此默认媒体类型更改为文件格式


注意:我使用的是Angular6。需要使用表单数据才能上载文件

const formData: FormData = new FormData();
formData.append('file', data, data.name); 
this.httpClient.post('api/data_loader/file/', formData);

我假设您的数据是formData,如果不是,那么在调用httpClient之前创建类似的表单数据

const data= new FormData();
data.append('file', data);  //note that this data is file not json data...
如果您正在发送formData,那么下面的函数应该可以工作

fileUpload(data){


          let headers = new Headers({ 
    });

//add any header if you need to for authentication 

return this.httpClient.post('api/data_loader/file/',data, headers);
}

只是一个提醒。内容类型
application/file
不是有效的媒体类型,请参阅:。我假定Angular或您的服务了解这一点,并将其映射到json。你甚至没有发送任何文件。发送文件的正确方法是Sachila.Hi Jyoti提供的答案,这个关于堆栈溢出的问题/答案可能会有所帮助