使用http发送图像到django restfull api

时间:2017-09-12 09:47:13

标签: django angular typescript angular2-services



fileChange(event) {
    debugger;
    let fileList: FileList = event.target.files;
    if (fileList.length > 0) {
      let file: File = fileList[0];
      let formData: FormData = new FormData();

      formData.append('uploadFile', file, file.name);
      let headers = new Headers()
      headers.append('Authorization', this.token);
      headers.append('Content-Type', 'application/json');
      headers.append('Content-Disposition', 'form-data; filename="'+file.name+'"');
      
      // Content-Disposition: form-data; name="fieldName"; filename="filename.jpg"

      headers.append('Content-Type', 'multipart/form-data');
      let options = new RequestOptions({ headers: headers });
      // let apiUrl1 = "/api/UploadFileApi";  
      this.http.post('http://192.168.1.160:8000/profilepic/', {FormData:formData}, options)
        .map(res => res.json())
        .catch(error => Observable.throw(error))
        .subscribe(
        data => alert('success'),
        error => alert(error)
        )
    }
    // window.location.reload();  
  }

<input class="uploadfile-style" [(ngModel)]="FilePath" (change)="fileChange($event)" name="CContractorFPath"                                                     
                                                size="10" type="file" enctype="multipart/form-data"> 
&#13;
&#13;
&#13;

斐伊川 我已经在角度2中为后期图像编写了此代码...但它显示错误不支持的媒体类型\&#34; application / json,multipart / form-data \&#34;在请求中 当我从邮递员发布它时接受相同的文件

以下是两个不同的卷曲命令

 1. This is accepted by Json

        curl -X POST \
      http://192.168.1.223:8010/profilepic/ \
      -H 'authorization: Basic YWRtaW46YWRtaW4=' \
      -H 'cache-control: no-cache' \
      -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
      -H 'postman-token: 794107c3-791d-e198-fe36-48f407a3ec8c' \
      -F datafile=@/home/ashwini/Pictures/b.jpeg


 2. This is not accepted by API



     curl 'http://192.168.1.223:8010/profilepic/' -H 'Authorization: 6df62b2d808c15acbdd8598d0153c8ca7e9ea28a' -H 'Origin: http://localhost:4201' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-GB,en-US;q=0.8,en;q=0.6' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36' -H 'Content-Type: application/json,multipart/form-data' -H 'Accept: application/json, text/plain, */*' -H 'Referer: http://localhost:4201/' -H 'Connection: keep-alive' --data-binary $'{\n  "FormData": {}\n}' --compressed

0 个答案:

没有答案