gpt4 book ai didi

javascript - 错误代码 2,安全错误 : typescript blob

转载 作者:行者123 更新时间:2023-12-03 02:36:58 27 4
gpt4 key购买 nike

我尝试将图像文件转换为 blob,但出现以下错误:

{"code":2, "message":"SECURITY_ERR"}

知道为什么会发生这种情况吗?我尝试了很多在线资源,但确实无法找出发生了什么。
我有以下代码:

choose(){
this.filechooser.open().then((uri)=>{

this.file.resolveLocalFilesystemUrl(uri).then((newurl)=>{

let dirPath = newurl.nativeURL;

alert(dirPath);

let dirPathsegments = dirPath.split('/')
dirPathsegments.pop();
dirPath = dirPathsegments.join('/');

this.file.readAsArrayBuffer(dirPath, newurl.name).then((buffer)=>{

let blob = new Blob([buffer], {type: "image/jpeg"});

alert('blob creation success');

}, Error=>{
alert('Blob Error '+ JSON.stringify(Error));
});

});
},Error=>{
alert('Error Choosing File ' + Error);
});
}

还有其他方法可以将文件转换为 blob 吗?我当前使用的 API 仅接受二进制或多部分附件

最佳答案

It works. Tested on ionic 2 & 3

uploadFile() {
this.fileChooser.open().then((url) => {

(<any>window).FilePath.resolveNativePath(url, (nativeFilepath) => {

this.readFileFromStorage(nativeFilepath);

}
)
})
}

readFileFromStorage(nativeFilepath) {

let fileName = this.getfilename(nativeFilepath);

let fileExt = fileName.substring(fileName.lastIndexOf('.') + 1);

let blogType = { type: 'image/'+fileExt };

(<any>window).resolveLocalFileSystemURL(nativeFilepath, (res) => {
res.file((resFile) => {
var reader = new FileReader();
reader.readAsArrayBuffer(resFile);
reader.onloadend = (evt: any) => {

var imgBlob = new Blob([evt.target.result], blogType);

//Upload blob to firebase
this.uploadToFirebase(imgBlob,fileName);
}
})
})
}

getfilename(filePath){
let fileName : string;
fileName = filePath.replace(/^.*[\\\/]/, '')
return fileName;
}

uploadToFirebase(fileBlob, name) {

let storage = firebase.storage();

storage.ref('images/' + name).put(fileBlob).then((d) => {
alert("Done");
}).catch((error) => {
alert("Error: " + JSON.stringify(error));
});

}

关于javascript - 错误代码 2,安全错误 : typescript blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48501330/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com