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:

<mat-form-field>
    <input matInput placeholder="Filename" [(ngModel)]="filename">
</mat-form-field>

<button type="button" mat-raised-button (click)="imgFileInput.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:

fileChange(event){

    let reader = new FileReader();
    let file = event.target.files[0];
    this.filename = file.name;
    reader.readAsDataURL(file);
    reader.onload = () => {
      let data = atob(reader.result.toString().split(',')[1]);
      this.is_file=false;
          var token = localStorage.getItem('token')
          let headers = new Headers({ 
            'Content-Type': 'application/json', 
            'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            'Access-Control-Allow-Origin':'*',
            '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;
          this.http.post(environment.API_URL + '/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(current_identity.id)

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

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