我需要找到一個解決方案,通過單個 axios POST 請求發送以下兩項:
- json結構
- 二進制檔案(excel檔案)
我怎樣才能做到這一點?
let files = event.target.files;
const fileReader = new FileReader();
fileReader.readAsText(files[0], null);
fileReader.onload = () => {
this.fileContent = fileReader.result;
let binaryDataForObject = this.fileContent;
let referenceDataStructure = {
textData: textDataForObject,
binaryData: binaryDataForObject,
referenceDataFileExtension: this.referenceDataFileExtension,
userProvidedDataTypes: this.columnTypes
};
}
this.axios
.post(
"http://url,
referenceDataStructure
)
這在技術上有效,但在 Java 方面我不知道如何解碼二進制資料(編碼為字串),以便將其視為 excel 檔案。
預先感謝您提供任何有意義的回復。盧博斯。
uj5u.com熱心網友回復:
- 通過簡單的
POST請求,您最多只能發送 1mb 的二進制資料 - 要在一個請求中發送二進制和文本,您應該使用
FormData
查看此答案以獲取資訊
14.12 更新
我在最近的專案中如何設法做到這一點是使用 FormData
所以首先你需要將檔案作為 blob 獲取:
const fileReader = new FileReader()
// Here we will get the file as binary data
fileReader.onload = () => {
const MB = 1000000;
const Blob = new Blob([fileReader.result], {
// This will set the mimetype of the file
type: fileInputRef.current.files[0].type
});
const BlobName = fileInputRef.current.files[0].name;
if (Blob.size > MB) return new Error('File size is to big');
// Initializing form data and passing the file as a param
const formData = new FormData();
// file - field name, this will help you to read file on backend
// Blob - main data to send
// BlobName - name of the file, default it will be name of your input
formData.append('file', Blob, BlobName);
// Append json data
formData.apped('some-key', someValue)
// then just send it as a body with post request
fetch('/api/submit-some-form-with-file', {
method: 'POST',
body: formData
})
// Handle the rest
.then()
}
fileReader.readAsArrayBuffer(fileInputRef.current.files[0])
您可以將此示例包裝在處理提交功能中的反應中,并按原樣使用或使用它
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/387259.html
標籤:javascript 爪哇 邮政 公理
上一篇:提交html表單到nodejs
下一篇:查找覆寫原始矩形的旋轉矩形的大小
