【问题标题】:upload and save image from google-drive api node.js从 google-drive api node.js 上传和保存图像
【发布时间】:2020-04-14 20:53:13
【问题描述】:

我是节点新手。我一直在挖掘试图找出如何路由我从谷歌驱动器 api 获得的传入图像。我试过下载它,但它需要很长时间。我希望能够在收到后立即将其发送给客户。这可能很简单,但我想做的就是将它保存到 png 文件中。 .蚂蚁帮助将不胜感激。

 app.get("/img", function(req,res){

 https.get(url,function(resp){
   console.log(resp.statusCode);
  console.log(resp.headers['content-type']);
   resp.on("data", function(chunk){
   // I am getting the image raw data  
 .pipe(fs.createWriteStream("public/images/test.png")); 

【问题讨论】:

  • 那么问题出在哪里?你的代码看起来不错?
  • 我收到此错误。 .pipe(fs.createWriteStream("public/images/test.png")); ^ SyntaxError: Unexpected token .

标签: javascript node.js express image-processing google-drive-api


【解决方案1】:

您可以将传入的数据直接通过管道传输到文件:

resp.pipe(fs.createWriteStream("public/images/test.png"))

你的代码将变成:

app.get("/img", function(req,res){
 https.get(url,function(resp){
        console.log(resp.statusCode);
        console.log(resp.headers['content-type']);
        resp.pipe(fs.createWriteStream("public/images/test.png"));
    });
});

【讨论】:

  • 这是把它放在文件中,但它是整个响应。我怎么能只点击图片?
  • 那么响应的内容类型是什么?它应该类似于 image/jpeg
  • 文本/html; charset=utf-8
  • 那么这个网址不是图片。确保 URL 是图片的直接链接,例如您的个人资料图片:lh6.googleusercontent.com/-poGft7zXvuc/AAAAAAAAAAI/AAAAAAAAAAA/…
  • 我可以使用您提供的下载以及“drive.google.com/uc?id="+fileId”访问它。它以 302 重定向响应我可以使用 resp.headers.location 来访问它,(@987654323 @) 将其分配给一个变量并立即发送另一个 get 请求。现在一切正常。我一直在做这个。感谢您的时间和帮助!
猜你喜欢
  • 2020-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-20
  • 2016-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多