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

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

问题描述

我已通过 node express 中的 API 将 base64 img 字符串上传到 Google Drive.上传 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.

base 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.
查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆