1

我正在使用 Dropbox SDK JS ( "dropbox": "^10.10.0") 通过 上传文件filesGetTemporaryUploadLink,此方法返回用于文件上传的临时 URL。

我上传文件的方式:

const fileUploadResult = await fetch(uploadDestination.url, {
    body: fileData,
    cache: "no-cache",
    credentials: "same-origin",
    headers: {
        "Content-Type": "application/octet-stream"
    },
    method: "POST",
    mode: "cors"
});

当我上传 PDF 时,一切正常,但是当我尝试上传.xlsx文件时,该文件以损坏的方式上传。

我试过玩Content-Type,但是当我更改application/octet-stream为 Excel 的 MIME ( application/vnd.openxmlformats-officedocument.spreadsheetml.sheet) 或 时binar/octet-stream,我收到错误:

加载资源失败:服务器响应状态为 400(错误请求)

POST这很奇怪,通过Fetch API发送 PDF 和 Excel 文件有什么区别?

4

1 回答 1

1

正如@Greg提到的那样,问题在于不必要地包装文件FormData,一旦我删除了这个包装,一切都正常工作:

const fileData = domRef.files[0];

const fileUploadResult = await fetch(uploadDestination.url, {
    body: fileData,
    cache: "no-cache",
    credentials: "same-origin",
    headers: {
        "Content-Type": "application/octet-stream"
    },
    method: "POST",
    mode: "cors"
});
于 2021-09-27T21:16:39.353 回答