简体   繁体   English

如何从服务器下载文件并在Node.js中同步重命名?

[英]How to download files from server and rename it synchronously in Node.js?

I want to every client can download files from the server's folder, and I don't want that all those files be saved in the /download folder. 我希望每个客户端都可以从服务器的文件夹下载文件,我不希望所有这些文件都保存在/ download文件夹中。

I thought first download the file and then rename it, but I need to run this code synchronously. 我想先下载文件,然后重命名,但我需要同步运行此代码。

router.get('/', function(req, res){

        const file = `${__dirname}/upload-folder/apple.jpg`;
        const destination = `C:/Users/steve/Downloads/apple.jpg`;

        res.download(file);

         fs.renameSync(destination,"C:/Users/steve/Desktop/downloadedimg.jpg");     
    }
);

It looks like you're conflating the client side vs. the server side. 看起来你正在混淆客户端和服务器端。 The code you provided is for node.js on the server side. 您提供的代码是服务器端的node.js。 You're not able to specify where on the client's computer a file is downloaded. 您无法指定客户端计算机上的文件下载位置。 You can provide a different name for the file that's being downloaded: 您可以为要下载的文件提供其他名称:

res.download(`${__dirname}/upload-folder/apple.jpg`, 'orange.jpg');

Reference: https://expressjs.com/en/api.html#res.download 参考: https//expressjs.com/en/api.html#res.download

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

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