我正在創建一個畫布來旋轉影像并創建一個 blob。但我需要它作為檔案上傳到服務器。
return new Promise((resolve, reject) => {
canvas.toBlob(blob => {
// returning an error
if (!blob) {
reject(new Error('Canvas is empty'));
return;
}
// blob.name = fileName;
// creating a Object URL representing the Blob object given
const rotatedImageUrl = window.URL.createObjectURL(blob);
resolve(rotatedImageUrl);
}, 'image/jpeg');
});
我正在嘗試從 blob 創建一個檔案。我可以使用這個片段來創建它。
const rotatedFile = new File([img], originalFile.name, {
type: originalFile.type,
});
但問題是生成的檔案大小只有58位元組。
lastModified: 1644577677380
lastModifiedDate: Fri Feb 11 2022 17:07:57 GMT 0600 (Bangladesh Standard Time) {}
name: "IMG_4792.JPG"
size: 58
type: "image/jpeg"
webkitRelativePath: ""
我認為影像以某種方式損壞。最初我認為畫布以某種方式破壞了我的影像,但我也嘗試從上傳的檔案創建一個檔案,轉換為 blob。這也給了我相同的輸出。檔案大小不正確。
const file = e.target.files[0];
const blobUrl = URL.createObjectURL(file);
const generatedFile = new File([blobUrl], 'untitled', {
type: file.type,
});
有什么解決辦法嗎?如何從 blob 正確創建檔案?
uj5u.com熱心網友回復:
你為什么使用createObjectURL?
blob您應該直接決議resolve(blob);,然后檔案將正確表示影像,而不是表示objectURL.
您看到的是生成的 url 的長度。就像是"blob:https://........'
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/427715.html
標籤:javascript 反应 文件 斑点
