2017-10-19 10 views
0

Blob 파일을 Firebase에 업로드하려고하는데 "makeFileIntoBlob"함수가 반환되면서 "undefined"가 계속 나타납니다. Android에서 실행 중입니다. 사진을 찍은 후 파일 경로를 가져올 수 있지만 Blob을 검색하여 Firebase에 업로드하는 기능을 호출하면 Blob 파일이 "정의되지 않은"파일이됩니다. 몇 도와달라고? Heres는Ilob 용 TypeScript on Blob에 파일 만들기

내가 그림

촬영 기능을 Heres
makeFileIntoBlob(imagePath) { 

var reader = new FileReader(); 
reader.onloadend = (evt: any) => { 
    var imgBlob: any = new Blob(imagePath); 
    imgBlob.name = 'sample.jpg'; 
    return imgBlob; 
}; 
reader.onerror = (e) => { 
    console.log('Failed file read: ' + e.toString()); 
}; 

FilePath를

에서의 Blob를 만들기 위해 노력하고 어디

uploadToFirebase(imageBlob) { 
var fileName = 'sample.jpg'; 
return new Promise((resolve, reject) => { 
var fileRef = firebase.storage().ref('Animais/' + fileName); 

var uploadTask = fileRef.put(imageBlob); 

uploadTask.on('state_changed', (snapshot) => { 
    console.log('snapshot progess ' + snapshot); 
}, (_error) => { 
    reject(_error); 
},() => { 
    // completion... 
    resolve(uploadTask.snapshot); 
}); 
}); 
} 

Heres는 중포 기지 저장하는 기능을 이미지를 업로드 기능

takePic(){ 

let imageSource; 

this.camera.getPicture({ 
    destinationType: this.camera.DestinationType.FILE_URI, 
    sourceType: imageSource, 
    encodingType: this.camera.EncodingType.JPEG, 
    targetHeight: 640, 
    correctOrientation: true, 
    saveToPhotoAlbum : true 
}).then((imagePath) => { 
    alert('got image path ' + imagePath); 
    // convert picture to blob 
    return this.makeFileIntoBlob(imagePath); 
}).then((imageBlob) => { 
    alert('got image blob ' + imageBlob); 
    // upload the blob 
    return this.uploadToFirebase(imageBlob); 
}).then((uploadSnapshot: any) => { 
    alert('file uploaded successfully ' + uploadSnapshot.downloadURL); 
    // store reference to storage in database 
    return this.saveToDatabaseAssetList(uploadSnapshot); 
}).then((uploadSnapshot: any) => { 
    //alert('file saved to asset catalog successfully '); 
}, (error) => { 
    alert('Error ' + (error.message || error)); 
}); 
+0

실제로 TypeScript를 사용하고 있습니까? 어떤 컴파일러 오류가 있습니까? 'BLOB'이란 무엇입니까? – jcalz

+0

예, Typescript, Ionic3. BLOB에 대해 미안 해요, 제가 일종의 디버깅 중이었습니다. 이미 편집했습니다. 난 컴파일러 오류가 아니야, 그냥 ""함수 "makeFileIntoBlob"의 반환으로 "정의되지"지고있다. –

+0

당신이'FileReader'를 사용하고 있기 때문에 이것은 [Javascript Promises with FileReader()] (https://stackoverflow.com/questions/34495796/javascript-promises-with-filereader)와 중복 될 수 있습니다. 콜백을 사용하여 '약속'을 해결하거나 거부하지 않습니다. – jcalz

답변

0

의견에서 말씀 드렸듯이 귀하의 문제는 FileReader이지만 해당 콜백을 사용하여 Promise을 확인/거부하지 않습니다. 대신 Promise을 반환해야합니다. 실제로 FileReader을 사용하여 imagePath의 내용을 읽지는 않지만, FileReaderFile 또는 Blob이 아니고 URI가 읽히기 때문에 작동하는지조차 알지 못합니다. URI가 data:// URI 인 경우 일부 코드로 Blob으로 변환 할 수 있습니다. URI가 file:// 인 경우 chrome-privileged이 아닌 이상 브라우저에서 수행 할 수있는 권한이 없습니다. 잠깐, 브라우저에있어?

불행히도 나는 당신을 생각하지 않는다. posted enough info for anyone to reproduce. Cordova을 사용하고 있습니까? 그렇다면 을 전달하는 대신 File Transfer API으로 반환 된 file:// URL을 사용하지 않으시겠습니까? 그게 효과가 없다면 나는 코르도바에 대해 아무것도 모르기 때문에 당신을 정말로 도울 수 없다. 다른 사람들이 귀하를 도울 수 있도록 태그를 추가 할 수 있습니다. 그리고 적용 가능한 다른 relevant questions에 대한 StackOverflow를 검색하십시오.

행운을 빈다.