上传到Google云端硬盘 - 如何使用base64编码的图片或图片路径 [英] Uploading to Google Drive - how to use a base64-encoded image or an image path
问题描述
我正在使用 http://ngcordova.com/docs/plugins/camera/ 在手机上捕捉图像,并可以检索为base64编码图像或作为图像位置的路径(file://storage/emulated/...jpg)。
接下来,我使用它将文件上传到Google drive: https://github.com/googledrive/cors-upload-sample
我已经成功上传了一个文本文件,并给出了示例:
* @example
* var content = new Blob([Hello world],{type:text / plain});
* var uploader = new MediaUploader({
* file:content,
* token:accessToken,
* onComplete:function(data){...}
* onError:function(data){...}
*});
* uploader.upload();
但是当我上传一张图片时,它无法被观看,我认为这是因为我已经编码了错误。
以下是我到目前为止所尝试的内容:使用
是否也可以使用FileReader将图像路径读取到File中?我只是无法弄清楚上面示例代码的图像的file:content部分究竟应该如何。
任何帮助将不胜感激!
上传期望的是原始图像字节。
Haven这并不是最好的方法,但最简单的方法可能是使用可以解析为文件的图像URL(这也是一个blob):
类似于: p>
window.resolveLocalFileSystemURL(uri,function(fileEntry){
var errorHandler = ...;
fileEntry .file(function(file){
var uploader = new MediaUploader({
file:file,
token:accessToken,
onComplete:function(data){...}
onError:function(data){...}
});
uploader.upload();
},errorHandler);
});
Fairly new to Javascript and still running into this problem after trying a lot of things:
I'm using http://ngcordova.com/docs/plugins/camera/ to capture an image on the phone and can retrieve as either a base64-encoded image or as a path to the image location (file://storage/emulated/...jpg).
Next I'm using this to upload files to google drive: https://github.com/googledrive/cors-upload-sample
I've successfully uploaded a text file with the given example:
* @example
* var content = new Blob(["Hello world"], {"type": "text/plain"});
* var uploader = new MediaUploader({
* file: content,
* token: accessToken,
* onComplete: function(data) { ... }
* onError: function(data) { ... }
* });
* uploader.upload();
But when I upload an image, it cannot be viewed and I assume it is because I have encoded it wrong.
Here's what I've tried so far:
- using the base64-encoded imagedata directly
- creating a blob of image/jpeg type and putting the 64encoded imagedata inside
- appending "data:image/jpeg;base64," in front of the imagedata
- this person's suggestion: https://stackoverflow.com/a/27197907/5568940
Is it also possible to use a FileReader to read the image path into a File instead? I just can't figure out what exactly should go into the "file: content" part for an image for the above example code.
Any help would be greatly appreciated!
The upload is expecting the raw image bytes.
Haven't tried this, but easiest approach is likely working with the image URL which you can resolve to a file (which is also a blob :)
Something like:
window.resolveLocalFileSystemURL(uri, function(fileEntry) {
var errorHandler = ...;
fileEntry.file(function(file) {
var uploader = new MediaUploader({
file: file,
token: accessToken,
onComplete: function(data) { ... }
onError: function(data) { ... }
});
uploader.upload();
}, errorHandler);
});
这篇关于上传到Google云端硬盘 - 如何使用base64编码的图片或图片路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!