Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.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
Javascript 在Azure函数中使用node作为响应发送zip文件的最佳方法?_Javascript_Node.js_Zip_Azure Functions - Fatal编程技术网

Javascript 在Azure函数中使用node作为响应发送zip文件的最佳方法?

Javascript 在Azure函数中使用node作为响应发送zip文件的最佳方法?,javascript,node.js,zip,azure-functions,Javascript,Node.js,Zip,Azure Functions,使用Azure function app,我希望能够将不同URL的图像下载到特定文件夹中,对其进行压缩,并在响应中发送回压缩文件 我可以通过以下步骤实现这一点: 对文件的请求 在本地保存文件 使用Zip压缩目录 archiver读取压缩文件,将其转换为base64 在响应体中发送缓冲区 下载并保存图像 const img = await request(url, { encoding: "binary" }); fs.writeFile(filesName, data, "binary", er

使用Azure function app,我希望能够将不同URL的图像下载到特定文件夹中,对其进行压缩,并在响应中发送回压缩文件

我可以通过以下步骤实现这一点:

  • 对文件的请求
  • 在本地保存文件
  • 使用Zip压缩目录
  • archiver读取压缩文件,将其转换为base64
  • 在响应体中发送缓冲区
  • 下载并保存图像

    const img = await request(url, { encoding: "binary" });
    fs.writeFile(filesName, data, "binary", err => {
        if (err) {
            reject(`Error while writing the file; ${err}`);
        } else {
            resolve(data);
        }
    });
    
    压缩目录,读取压缩文件并发送响应

    const target = await zipDirectory(dirName, targetFile);
    context.log('Target ' + targetFile);
    const rawFile = await readFile(targetFile);
    const fileBuffer = Buffer.from(rawFile, "base64");
    context.res = {
        body: fileBuffer,
        headers: {
        "Content-Disposition": `filename=target.zip`,
        "Content-Type": "application/zip"
    },
    status: 202
    };
    
    有更好的方法吗

  • 创建一个带有
    http
    触发器的函数,其中输入是图像的uri,输出绑定是blob容器。逻辑是将图像保存在blob存储器中

  • 创建另一个blob触发的函数,该函数将抓取文件并对其进行压缩,它可以具有输出blob绑定。它将压缩文件并将其放入输出blob绑定中

  • 压缩文件将位于输出blob容器中

  • 或者,您可以使用持久功能来协调整个流程