简体   繁体   English

上传到Google云端硬盘-如何使用base64编码的图像或图像路径

[英]Uploading to Google Drive - how to use a base64-encoded image or an image path

Fairly new to Javascript and still running into this problem after trying a lot of things: 对Javascript来说还很陌生,在尝试了很多尝试之后仍然遇到了这个问题:

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). 我正在使用http://ngcordova.com/docs/plugins/camera/在手机上捕获图像,并且可以将其检索为base64编码的图像或图像位置的路径(file:// storage /仿真/...jpg)。

Next I'm using this to upload files to google drive: https://github.com/googledrive/cors-upload-sample 接下来,我正在使用此文件将文件上传到Google驱动器: 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 直接使用base64编码的imagedata
  • creating a blob of image/jpeg type and putting the 64encoded imagedata inside 创建图片/ jpeg类型的斑点并将64编码的图像数据放入其中
  • appending "data:image/jpeg;base64," in front of the imagedata 在imagedata前面附加“ data:image / jpeg; base64”
  • this person's suggestion: https://stackoverflow.com/a/27197907/5568940 此人的建议: https : //stackoverflow.com/a/27197907/5568940

Is it also possible to use a FileReader to read the image path into a File instead? 是否还可以使用FileReader代替将图像路径读取到File中? 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 :) 尚未尝试过,但是最简单的方法可能是处理图像URL,您可以将其解析为文件(也是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); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何下载 base64 编码的图像? - How to download a base64-encoded image? IPFS:base64 编码的图像未显示为图像 - IPFS: base64-encoded image not showing as image IE10-base64编码的图像加载错误 - IE10 - base64-encoded image load error 使用angularjs将SVG中的xlink:href设置为base64编码的图像 - Set xlink:href in svg with angularjs to base64-encoded image Sails.js如何将base64编码的图像转换为图像并将其存储在磁盘中? - Sails.js how to convert a base64-encoded to image and store it in disk? 如何从 javascript 中的本地文件图像创建 base64 编码的字符串 - How to create a base64-encoded string from a local file image in javascript 给定一个画布对象,我如何获得它的按比例缩小的“版本”作为图像/ Base64编码的字节数组? - Given a canvas object, how can I get a scaled down “version” of it as an image/Base64-encoded byte array? 如何在sails.js 或通常在服务器端JavaScript 中将图像转换为base64 编码的数据URL? - How do I convert an image to a base64-encoded data URL in sails.js or generally in the servers side JavaScript? 如何添加路径到base64编码的图像 - How to add path to a base64 encoded image 对 base64 URI 支持的更好测试(我可以在 JS 中创建一个大的 base64 编码图像吗?) - A better test for base64 URI support (can I create a large base64-encoded image in JS?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM