簡體   English   中英

在Ubuntu中使用fetch和formdata上傳文件成功,但在Windows 10中失敗

[英]Uploading a file using fetch and formdata succeeds in Ubuntu but failed in Windows 10

伙計們 這些天我遇到了一個令人討厭的問題。

我正在嘗試使用FormData類型中的訪存將文件上傳到服務器端,如下所示:

export function buildFormData(request) {
  const formData = new window.FormData();
  for (const name in request) {
    formData.append(name, request[name]);
  }
  return formData;
}

接着

export function checkImported(file) {
  const formData = UTILS.buildFormData({ file });
  return fetch(`${ServerConst.SERVER_CONTEXT_PATH}/import/check`, {
    method: 'POST',
    body: formData,
  }).then(response => response.json());
}

Ubuntu中 ,請求為:

在此處輸入圖片說明

Windows 10中是:

在此處輸入圖片說明

我所不同地檢查的是請求有效負載 ,在Windows 10中是應用程序/八位字節流 ,而在Ubuntu中是text / csv

但是,當我嘗試為提取添加標頭時 ,如下所示:

export function checkImported(file) {
  const formData = UTILS.buildFormData({ file });
  return fetch(`${ServerConst.SERVER_CONTEXT_PATH}/import/check`, {
    method: 'POST',
    headers: { 'Content-Type': 'multipart/form-data; text/csv' },
    body: formData,
  }).then(response => response.json());
}

,它也會在ubuntu中失敗(服務器端會提示我有關the request was rejected because no multipart boundary was found 。並且在這篇文章中對此原因進行了清楚的解釋。

有人可以幫忙嗎? 非常感謝你!

最好的祝福,聽見。

這是由於java中的后端驗證所致,該驗證將過濾掉僅保留“ Content-Type:text / csv”的其他類型。

如您所見,在Windows中, Request Payload的Content-Type是application / octet-stream

由於前端可以通過上載中的accept過濾掉所有非csv文件,因此我刪除了后端中的驗證並解決了問題。

暫無
暫無

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

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