So laden Sie FILE_URI über die Google Drive-API hoch: Datei einfügen
Unter Android versuche ich, die Ausgabe von Cordova / Phonegap getPicture () mithilfe der Google Drive-API hochzuladen: Datei einfügen. Gibt es eine Möglichkeit, dies mithilfe von FILE_URI anstelle von DATA_URL (base64) zu tun?
Ich habe zuerst Camera.DestinationType.DATA_URL ausprobiert, aber Base64-Daten wurden nicht wie erwartet zurückgegeben, sondern nur das Gleiche wie FILE_URI. Jetzt versuche ich herauszufinden, wie FILE_URI an Google Drive Insert File übergeben wird (für das Base64 erforderlich ist). Gibt es eine Möglichkeit, FILE_URI nach Base64 zu konvertieren?
Cordova-Code:
navigator.camera.getPicture(onSuccess, onFail,
{ quality: 50, destinationType: Camera.DestinationType.FILE_URI });
function onSuccess(imageURI) {
var image = document.getElementById('myImage');
image.src = imageURI;
// need to do something like this:
var fileData = ConvertToBase64(imageURI);
insertFile(fileData);
}
Google Drive-Code:
/**
* Insert new file.
*
* @param {File} fileData File object to read data from.
* @param {Function} callback Function to call when the request is complete.
*/
function insertFile(fileData, callback) {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function(e) {
var contentType = fileData.type || 'application/octet-stream';
var metadata = {
'title': fileData.fileName,
'mimeType': contentType
};
var base64Data = btoa(reader.result);
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody});
if (!callback) {
callback = function(file) {
console.log(file)
};
}
request.execute(callback);
}
}