我想從我選擇的檔案 (pdf) 中提取位元組碼以將其保存在我的資料庫中。但我總是收到我的位元組未定義的錯誤。有人可以看看我的代碼并告訴我它有什么問題嗎?
我嘗試使用 FileReader,但我的位元組回傳未定義,formdata 作業正常,它在控制臺上顯示我需要的檔案的所有資訊。
編輯:
uploadFile2(files: FileList | null): void {
const file = files.item(0)
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => {
this.documentsArray.push({documentType: this.form.controls.dokumentType.value, file: reader.result})
console.log(this.documentsArray)
}
}
嗨,我編輯了我的代碼,現在我得到了一個 base64,我想,但有一個問題,它是這樣開始的:data:application/pdf;base64,JVBERi0xLjQKJfbk/N8KM ......”
以 data:application/pdf 開頭是正確的還是我必須更改某些內容才能將其保存在資料庫中
uj5u.com熱心網友回復:
我建議您將檔案作為 base64 字串存盤在資料庫中。這將如下所示。使用第 2 行,您從輸入事件中獲取檔案。
const reader = new FileReader();
reader.readAsDataURL(event.target.files[0]);
reader.onload = (event) => {
if (reader.result) {
//save pdf base64 into database
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/352691.html
標籤:javascript 有角的 打字稿
