gpt4 book ai didi

javascript - Node/Express 生成一次性路由/链接/下载?

转载 作者:数据小太阳 更新时间:2023-10-29 05:50:01 25 4
gpt4 key购买 nike

我将如何在 nodeJS 或 Express 中创建一次性下载链接?

我正在尝试找到实现此目的的最简单方法。到目前为止,我的想法是:

使用fs流读取然后删除文件或者以某种方式生成一个链接/路由,一旦单击下载按钮,该链接/路由就会被删除

这些实现是否可行?有没有更简单的方法?

任何帮助或示例代码将不胜感激!

-谢谢

最佳答案

检查这个简单的实现:

您将下载信息存储在一个文件中。文件名是下载 session ID。文件内容为要下载文件的真实路径。

使用这三个函数来管理下载 session 的生命周期:

var fs     = require('fs');
var crypto = require('crypto');
var path = require('path');

// Path where we store the download sessions
const DL_SESSION_FOLDER = '/var/download_sessions';

/* Creates a download session */
function createDownload(filePath, callback) {
// Check the existence of DL_SESSION_FOLDER
if (!fs.existsSync(DL_SESSION_FOLDER)) return callback(new Error('Session directory does not exist'));

// Check the existence of the file
if (!fs.existsSync(filePath)) return callback(new Error('File doest not exist'));

// Generate the download sid (session id)
var downloadSid = crypto.createHash('md5').update(Math.random().toString()).digest('hex');

// Generate the download session filename
var dlSessionFileName = path.join(DL_SESSION_FOLDER, downloadSid + '.download');

// Write the link of the file to the download session file
fs.writeFile(dlSessionFileName, filePath, function(err) {
if (err) return callback(err);

// If succeeded, return the new download sid
callback(null, downloadSid);
});
}

/* Gets the download file path related to a download sid */
function getDownloadFilePath(downloadSid, callback) {
// Get the download session file name
var dlSessionFileName = path.join(DL_SESSION_FOLDER, downloadSid + '.download');

// Check if the download session exists
if (!fs.existsSync(dlSessionFileName)) return callback(new Error('Download does not exist'));

// Get the file path
fs.readFile(dlSessionFileName, function(err, data) {
if (err) return callback(err);

// Return the file path
callback(null, data);
});
}

/* Deletes a download session */
function deleteDownload(downloadSid, callback) {
// Get the download session file name
var dlSessionFileName = path.join(DL_SESSION_FOLDER, downloadSid + '.download');

// Check if the download session exists
if (!fs.existsSync(dlSessionFileName)) return callback(new Error('Download does not exist'));

// Delete the download session
fs.unlink(dlSessionFileName, function(err) {
if (err) return callback(err);

// Return success (no error)
callback();
});
}

使用 createDownload()随时随地创建下载 session 。它返回下载 sid,然后您可以使用它来构建您的下载 URL,例如:http://your.server.com/download?sid=<RETURNED SID> .

最后,您可以向 /download 添加一个简单的处理程序路线:

app.get('/download', function(req, res, next) {
// Get the download sid
var downloadSid = req.query.sid;

// Get the download file path
getDownloadFilePath(downloadSid, function(err, path) {
if (err) return res.end('Error');

// Read and send the file here...

// Finally, delete the download session to invalidate the link
deleteDownload(downloadSid, function(err) {
// ...
});
});
});

使用这种方法,您不必创建/移动/删除大的下载文件,这可能会导致响应缓慢和不必要的资源消耗。

关于javascript - Node/Express 生成一次性路由/链接/下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21999877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com