Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

291
Views
Código para crear un enlace de descarga que vence usando nodejs o express [frontend y backend]

Soy nuevo aqui. Acabo de empezar a usar node.js - express.js Quiero agregar un enlace de descarga que caduque después de cierto tiempo en mi sitio web. Estoy luchando para codificar esto desde hace una semana. Cualquiera que sepa, proporcione el código [frontend y backend], será de gran ayuda.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Compruebe esta implementación simple:

Almacenas la información de la descarga en un archivo. El nombre de archivo es la identificación de la sesión de descarga. El contenido del archivo es la ruta real del archivo a descargar.

Utilice estas tres funciones para gestionar el ciclo de vida de las sesiones de descarga:

 /* 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(); }); }

Use createDownload() para crear sesiones de descarga donde lo necesite. Devuelve el sid de descarga, luego puede usarlo para crear su URL de descarga como: http://your.server.com/download?sid= .

Finalmente, puede agregar un controlador simple a su ruta /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) { // ... }); }); });

Con este método, no tiene que crear/mover/eliminar archivos de descarga grandes, lo que podría causar respuestas lentas y un consumo de recursos innecesario.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!