通过驱动器 api 上传时无法查看 Base64 字符串 img [英] Base64 string img not viewable when uploaded via drive api

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

问题描述

我已通过 node express 中的 API 将 base64 img 字符串上传到 Google Drive.上传 img 后,在 Drive 中看不到它.我不确定如何解决这个格式问题.我知道我可以先在本地保存 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天全站免登陆