通过驱动器api上传时,Base64字符串img不可见 [英] Base64 string img not viewable when uploaded via drive api

查看:65
本文介绍了通过驱动器api上传时,Base64字符串img不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已通过node express中的API将base64 img字符串上传到Google云端硬盘.上载img之后,在云端硬盘中将无法看到该img.我不确定如何解决此格式问题.我知道我可以先在本地保存img,然后再上传保存的img文件,但我希望有一种更简单的方法.

I have uploaded a base64 img string to Google Drive via API in node express. After uploading the img, it is not viewable in Drive. I'm not sure on how to resolve this formatting issue. I know I could potentially save the img locally first, then upload the saved img file but I was hoping there is a simpler way.

我的代码:

 const uploadImg = async (folderId,img)=>{


 process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0


 const scopes = [
     'https://www.googleapis.com/auth/drive'
 ];
 const auth = new google.auth.JWT(
     demoApiCreds.client_email, null,
     demoApiCreds.private_key, scopes
 );

 const drive = google.drive({ version: 'v3', auth });

 const fileMetadata = {
     'name': 'Client_Design_ScreenShotTest',
     'mimeType':'image/jpeg',
     'parents':[folderId]
 };


const uploadImg = img.split(/,(.+)/)[1];

const media = {
     body: uploadImg
 }

 let res = await drive.files.create({
     resource: fileMetadata,
     media: media,
     fields: 'id',
 });
 console.log('the response is',res);
 console.log('the data is ',res.data);
 return res.data;

}

该文件以jpg格式存储在驱动器中,但img为空白,之后为单击img谷歌驱动器抱怨该文件不能被读.下载后,img仍为空白.

The file is stored in drive, as a jpg, but the img is blank and after the img is clicked google drive complains that the file cannot be read. The img is still blank after downloading.

64位img字符串为

The base 64 img string is

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAhAAAADqCAYAAADzlnzfAAAAAXNSR0I...

我按照其他线程中的建议在上传之前删除了data:image/png; base64.无论是否带有此前缀,它都会失败.

I remove data:image/png;base64 before uploading as has been suggested in other threads. It fails with or without this prefix.

推荐答案

  • 您要使用带有node.js的googleapis将图像上传到Google云端硬盘.
  • img 的图像是base64数据.
  • 您已经能够使用Drive API将文件上传和下载到Google Drive.
    • You want to upload an image to Google Drive using googleapis with node.js.
    • The image of img is the base64 data.
    • You have already been able to upload and download files to Google Drive using Drive API.
    • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个答案之一.

      If my understanding is correct, how about this answer? Please think of this as just one of several answers.

      • 不幸的是,当使用googleapis上传base64数据时,base64数据未解码,并且该数据作为文本数据上传.因此,当您看到上传的文件时,您将无法看到它作为图像.如果可以将 Content-Transfer-Encoding:base64 添加到请求正文中base64数据的标头中,则base64数据将被转换并作为图像上传.但是当使用googleapis时,在当前阶段还无法实现.
      • Unfortunately, when the base64 data is uploaded using googleapis, the base64 data is not decoded and the data is upload as the text data. So when you see the uploaded file, you cannot see it as the image. If Content-Transfer-Encoding: base64 can be added to the header of the base64 data in the request body, the base64 data is converted and uploaded as an image. But when googleapis is used, in the current stage, it cannot be achieved.

      要将从图像编码的base64数据作为图像上传到Google云端硬盘,如何进行以下修改?

      In order to upload the base64 data encoded from an image as an image to Google Drive, how about the following modification?

      在此修改中,base64图像被转换为​​流类型,并被上载.请按如下所示修改脚本.

      In this modification, the base64 image is converted to the stream type, and uploaded. Please modify your script as follows.

      const uploadImg = img.split(/,(.+)/)[1];
      
      const media = {
        body: uploadImg
      }
      

      至:

      const stream = require("stream"); // Added
      
      const uploadImg = img.split(/,(.+)/)[1];
      const buf = new Buffer.from(uploadImg, "base64"); // Added
      const bs = new stream.PassThrough(); // Added
      bs.end(buf); // Added
      
      const media = {
        body: bs // Modified
      };
      

      注意:

      • 即使在 fileMetadata 上使用了'mimeType':'image/jpeg',该图像文件也会作为 image/png 上载.但是例如,如果在 fileMetadata 上使用了'mimeType':'application/pdf',则图像文件将作为 application/pdf 上载.请注意这一点.因此,我还建议修改为 10100111001的答案中提到的'mimeType':'image/png'a>.
      • 在"googleapis@43.0.0"上,资源:fileMetadata requestBody:fileMetadata 的两种模式都可以工作.
      • Note:

        • Even if 'mimeType':'image/jpeg' is used at fileMetadata, the image file is uploaded as image/png. But for example, if 'mimeType':'application/pdf' is used at fileMetadata, the image file is uploaded as application/pdf. Please be careful this. So I also recommend to modify to 'mimeType':'image/png' as mentioned by 10100111001's answer.
        • At "googleapis@43.0.0", both patterns of resource: fileMetadata and requestBody: fileMetadata work.
        • 如果我误解了您的问题,而这不是您想要的方向,我深表歉意.

          If I misunderstood your question and this was not the direction you want, I apologize.

          这篇关于通过驱动器api上传时,Base64字符串img不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆