Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从Dropbox流读取到缓冲区_Node.js_Dropbox - Fatal编程技术网

Node.js 从Dropbox流读取到缓冲区

Node.js 从Dropbox流读取到缓冲区,node.js,dropbox,Node.js,Dropbox,我需要从Dropbox下载一个文件到我服务器上的缓冲区。由于安全问题,我无法将文件直接下载到客户端。因此,我向服务器发送请求,然后从Dropbox中获取文件,然后将其转发给客户端。我设法实现了将Dropbox流写入服务器上的文件,然后将其发送到客户端 export const downloadFileFromDropbox = async function downloadFileFromDropbox(fileName,

我需要从Dropbox下载一个文件到我服务器上的缓冲区。由于安全问题,我无法将文件直接下载到客户端。因此,我向服务器发送请求,然后从Dropbox中获取文件,然后将其转发给客户端。我设法实现了将Dropbox流写入服务器上的文件,然后将其发送到客户端

export const downloadFileFromDropbox = async function downloadFileFromDropbox(fileName,
                                                                              folderNameOnDropbox) {

    let isSucceeded;
    const message = [];
    let downloadResult;
    let fileBuffered = "";

    // authentication
    const dropbox = dropboxV2Api.authenticate({
        token: process.env.DEV_DROPBOX_SECRET_KEY
    });

    // configuring parameters
    const params = Object.freeze({
        resource: "files/download",
        parameters: {
            path: `/${folderNameOnDropbox}/${fileName}`
        }
    });


      let dropboxPromise = new Promise(function (resolve, reject) {
            dropbox(params, function (err, result) {
                if (err) {
                    reject(err);
                } else {
                    resolve(result);
                }
        }).pipe(fs.createWriteStream(/* need to implement buffer here */));
     });

     await dropboxPromise.then(async function (resultObj) {
            isSucceeded = true;
            message.push("fileDownload_OK");
        }).catch(async function (err) {
            isSucceeded = false;
            message.push(err.message);
     });

    downloadResult = {
        isSucceeded,
        message,
        /* Buffer */
    };

    return downloadResult;

};
我需要在不将Dropbox流写入服务器上的文件的情况下实现此机制。我需要创建一个缓冲区并写入其中,然后将缓冲区转发给客户机

export const downloadFileFromDropbox = async function downloadFileFromDropbox(fileName,
                                                                              folderNameOnDropbox) {

    let isSucceeded;
    const message = [];
    let downloadResult;
    let fileBuffered = "";

    // authentication
    const dropbox = dropboxV2Api.authenticate({
        token: process.env.DEV_DROPBOX_SECRET_KEY
    });

    // configuring parameters
    const params = Object.freeze({
        resource: "files/download",
        parameters: {
            path: `/${folderNameOnDropbox}/${fileName}`
        }
    });


      let dropboxPromise = new Promise(function (resolve, reject) {
            dropbox(params, function (err, result) {
                if (err) {
                    reject(err);
                } else {
                    resolve(result);
                }
        }).pipe(fs.createWriteStream(/* need to implement buffer here */));
     });

     await dropboxPromise.then(async function (resultObj) {
            isSucceeded = true;
            message.push("fileDownload_OK");
        }).catch(async function (err) {
            isSucceeded = false;
            message.push(err.message);
     });

    downloadResult = {
        isSucceeded,
        message,
        /* Buffer */
    };

    return downloadResult;

};

如果不将文件写入某个位置,就无法将其转换为缓冲区 对我起作用的是:

  • 将文件写入临时文件夹
  • 读取并转换为缓冲区
  • 将缓冲区发送回客户端
  • 从临时位置删除该文件
  • 这是我的密码:

       dropbox = dropboxV2Api.authenticate({ token: credentials.access_token });
    dropbox(
      {
        resource: 'files/download',
        parameters: {
          path: `${req.query.folder}` + `/${req.query.filename}`,
        },
      },
      (err, result, response) => {
        //download completed
        if (err) {
          return res.status(500).send(err);
        } else {
          console.log('download completed');
        }
      }
    ).pipe(
      fs
        .createWriteStream(`./app/tmp/` + `${req.query.filename}`)
        .on('finish', () => {
          fs.readFile(
            `./app/tmp/` + `${req.query.filename}`,
            function (err, buffer) {
              if (err) {
                res.status(500).send(err);
              } else {
                fs.unlink(`./app/tmp/` + `${req.query.filename}`, (err) => {
                  if (err) {
                    console.error(err);
                    return;
                  }
                  //file removed
                  res.status(200).send(buffer);
                });
              }
            }
          );
        })
    );
    

    我希望这将帮助您,即使有点晚了

    如果不将文件写入某个位置,就无法将其转换为缓冲区 对我起作用的是:

  • 将文件写入临时文件夹
  • 读取并转换为缓冲区
  • 将缓冲区发送回客户端
  • 从临时位置删除该文件
  • 这是我的密码:

       dropbox = dropboxV2Api.authenticate({ token: credentials.access_token });
    dropbox(
      {
        resource: 'files/download',
        parameters: {
          path: `${req.query.folder}` + `/${req.query.filename}`,
        },
      },
      (err, result, response) => {
        //download completed
        if (err) {
          return res.status(500).send(err);
        } else {
          console.log('download completed');
        }
      }
    ).pipe(
      fs
        .createWriteStream(`./app/tmp/` + `${req.query.filename}`)
        .on('finish', () => {
          fs.readFile(
            `./app/tmp/` + `${req.query.filename}`,
            function (err, buffer) {
              if (err) {
                res.status(500).send(err);
              } else {
                fs.unlink(`./app/tmp/` + `${req.query.filename}`, (err) => {
                  if (err) {
                    console.error(err);
                    return;
                  }
                  //file removed
                  res.status(200).send(buffer);
                });
              }
            }
          );
        })
    );
    
    我希望这会对你有所帮助,尽管有点晚了