Linked Questions

Popular Questions

How do I upload a excel .xlsx file to python flask from angular2?

I upload something but it can't be read when I open the excel file.

html for upload dialog:

    <input matInput placeholder="Filename" [(ngModel)]="filename">

<button type="button" mat-raised-button (click)="" [disabled]="!is_file">Upload file</button>
<input hidden type="file" #imgFileInput (change)="fileChange($event)" accept=".xlsx"/>

<button type="button" mat-raised-button [disabled]="is_file">Submit</button>

ts code for sending post to the api:


    let reader = new FileReader();
    let file =[0];
    this.filename =;
    reader.onload = () => {
      let data = atob(reader.result.toString().split(',')[1]);
          var token = localStorage.getItem('token')
          let headers = new Headers({ 
            'Content-Type': 'application/json', 
            'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Allow-Methods': 'POST',
            'Authorization': 'bearer ' + token
           let options = new RequestOptions({ headers: headers });
           options.responseType = ResponseContentType.Blob;

          let ran = Math.floor(Math.random() * 10000) + 1;
 + '/test/model/expsoure/excel/' + 'model_id' + '?cache=' + ran.toString(),data,options)
          .subscribe(service_data => {

  }// end fileChange

Python code for saving data:

class exposureExcelId(Resource):

      method_decorators = [jwt_required()]

      def post(self,mid):

          user_id = str(

          f = open('/Users/data/wtf.xlsx','wb')

Error I get when I try and load the file from the upload in excel is:

excel could not open wtf.xlsx because some of the content is unreadable

Related Questions