简体   繁体   English

如何在Node.js后端中使用Dropbox API v2上传图像

[英]How do I upload images using dropbox API v2 in nodejs backend

I've been trying to upload images to a dropbox app folder using the dropbox API v2 in my nodejs backend but every image that uploads successfully is corrupted and is only 15 bytes large and cannot be previewed from within the dropbox app folder. 我一直在尝试使用nodejs后端中的dropbox API v2将图像上传到dropbox应用程序文件夹,但是成功上传的每个图像都已损坏,并且只有15个字节大,无法在dropbox应用程序文件夹中进行预览。

This is my code; 这是我的代码;

const express = require("express");
const router = express.Router();
const fetch = require("isomorphic-fetch");
const Dropbox = require("dropbox").Dropbox;

const dbx = new Dropbox({
  accessToken:
    "my access token",
  fetch: fetch
});


router.post("/dbx", (req, res) => {

  let imageArray;

//I receive either a single image or an array of images from the front end and
//its placed in req.files by express-fileupload

  if (req.files.itemImage.length) {
    imageArray = [...req.files.itemImage];
  } else {
    imageArray = [req.files.itemImage];
  }

  imageArray.forEach(image => {
  console.log("Image==>>",image)

 dbx
      .filesUpload({
        path: `/${image.name}`,
        contents: image
      })
      .then(response => {
        console.log(response);
      })
      .catch(err => {
        console.log(err);
      });
});
});

Below is what a typical image that I'm sending looks like from the command console.log("Image==>>",image) in my terminal 以下是我要从终端中的console.log("Image==>>",image)命令发送的典型图像的外观

{ name: 'ImageName.jpg',
  data:
  <Buffer ff d8 ff e1 1f 39 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 07 01 12 00 03 00 00 00 01 00 01 00 00 01 1a 00 05 00 00 00 01 00 00 00 62 01 1b 00 05 ... >,
  size: 1865542,
  encoding: '7bit',
  tempFilePath: '',
  truncated: false,
  mimetype: 'image/jpeg',
  md5: '133bda2ce6d52b4bada8ba31349c4910',
  mv: [Function: mv] }

Below is the response I receive from dropbox in my terminal 以下是我从终端机中的保管箱收到的回复

{ name: 'ImageName.jpg',
  path_lower: '/imageName.jpg',
  path_display: 'ImageName.jpg',
  id: 'id:R0fPv_de0wAAAAAAAAAAJw',
  client_modified: '2019-07-08T10:21:36Z',
  server_modified: '2019-07-08T10:21:36Z',
  rev: '0130000000015cb7a8c0',
  size: 15,
  is_downloadable: true,
  content_hash:
   '87bfbb905f228845cfb691920551882f446042da2ef9576e26ca97c5374c3eef' }

The image name shows up correctly in the name: field, but the image is only 15bytes in size and cannot be previewed from within the dropbox app folder. 图片名称正确显示在name:字段中,但是图片只有15个字节,无法在Dropbox应用程序文件夹中预览。 I don't understand what's going wrong hence I have no idea how to fix it. 我不明白发生了什么问题,因此不知道如何解决。 Could anyone please help me explain what I'm doing wrong? 谁能帮我解释我做错了什么吗? If anything about my question isn't clear, please point it out and I'll edit accordingly. 如果不清楚我的问题,请指出来,我将进行相应的编辑。

I tried sending the contents of the request as JSON.stringify(image) and got a corrupted file 3 times bigger than what I sent, I saw a method where I'd have to first upload the images to a folder on my server then use fs commands to write the contents of each one into the request, but I don't want to have such a folder on the server, I'd rather just send the images immediately if possible. 我尝试将请求的内容作为JSON.stringify(image)发送,并且损坏的文件比发送的文件大3倍,我看到了一种方法,必须首先将图像上传到服务器上的文件夹,然后使用fs命令将每个内容写入请求,但是我不想在服务器上有这样的文件夹,我宁愿在可能的情况下立即发送图像。

I expect to receive a positive message from the dropbox server with the correct image size and then be able to preview the image within the dropbox app folder I created. 我希望收到来自保管箱服务器的肯定消息,其中包含正确的图像大小,然后可以在我创建的保管箱应用程序文件夹中预览图像。

I figured it out myself. 我自己弄清楚了。 Only posting in case anyone ever faces the same problem. 仅在万一有人遇到相同问题的情况下发布。 The contents should be changed from image, to image.data 内容应从image更改为image.data

ie rather than doing this; 即而不是这样做;

dbx
      .filesUpload({
        path: `/${image.name}`,
        contents: image
      })
      .then(response => {
        console.log(response);
      })
      .catch(err => {
        console.log(err);
      });

it should be this 应该是这个

dbx
      .filesUpload({
        path: `/${image.name}`,
        contents: image.data
      })
      .then(response => {
        console.log(response);
      })
      .catch(err => {
        console.log(err);
      });

Posting this as an FYI for future searchers because I had a hard time understanding this, too. 将其作为供将来搜索者参考的信息,因为我也很难理解这一点。 Comments are welcome if this explanation can be improved. 如果可以改进此解释,欢迎发表评论。
Dropbox's /files/upload endpoint is expecting the binary data of the file (among other things). Dropbox的/ files / upload端点需要文件的二进制数据(除其他外)。 In the server-side JavaScript world, the Buffer class is how Node deals with binary data. 在服务器端JavaScript领域, Buffer类是Node处理二进制数据的方式。 While you can sometimes spot a file's binary data ( Buffer object) in the request or response, the Buffer class also comes with a variety of methods to make your own Buffer object out of a file when you need to. 尽管有时您可以在请求或响应中发现文件的二进制数据( Buffer对象),但Buffer类还附带了多种方法,可以根据需要在文件中创建自己的Buffer对象。
I found this article about Buffers from FreeCodeCamp to be a helpful resource. 我发现有关FreeCodeCamp中的缓冲区的这篇文章是有用的资源。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM