简体   繁体   中英

How to set content type while uploading a file

I am uploading a file to s3 bucket. While the uploading need to find file extension and need to add it correct content type.

$('#upload-files').on('click', function(e) {
  e.preventDefault();
  var fileName = data.files[0].name;
  var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
  if (ext == "pdf") {
    console.log(data);
    data.submit();
  } else {
    alert("Upload Pdf images only");
    return false;
  }
})

How to add its content-type before upload.

This code will help you to send data to server:

public formdata = new FormData();
onSubmit() {
  this.resetform(); //Order matters here
  let headers: any = new Headers();
  headers.append('Content-type', 'undefined');

  formData.append("selectFile", this.formData);
  const req5 = new HttpRequest('POST', 'url as hosted on TOMCAT', formData,
    reportProgress: true,
    responseType: 'text'
  });

  return this.httpClient.request(req5).subscribe(e => {
    (console.log(e);)
  }
}

resetform() {
  this.formData = new FormData();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM