簡體   English   中英

如何在上傳文件時設置內容類型

[英]How to set content type while uploading a file

我正在將文件上傳到s3存儲桶。 雖然上傳需要查找文件擴展名並需要添加正確的內容類型。

$('#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;
  }
})

如何在上傳之前添加其內容類型。

此代碼將幫助您將數據發送到服務器:

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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM