javascript - setting file name for download response in node.js -


how can set file name downloaded binary in following node response request,for downloads file , set name req.url string

.get(function (req, res) {   var filename = path.join(process.cwd(), '');   path.exists(filename, function (exists) {     if (!exists) {       res.writehead(404, {         "content-type": "text/plain"       });       res.write("file not found: 404 not found\n");       res.end();       return;     }      if (fs.statsync(filename).isdirectory()) {       filename += '/' + category + '/' + 'undo.png';     }      fs.readfile(filename, "binary", function (err, file) {       if (err) {         res.writehead(500, {           "content-type": "binary"         });         res.write(err + "\n");         res.end();         return;       }        res.writehead(200);       res.write(file, "binary");       res.end();     });   }); }); 

.get(function (req, res) {   var filename = path.join(process.cwd(), '');   path.exists(filename, function (exists) {     if (!exists) {       res.writehead(404, {         "content-type": "text/plain"       });       res.write("file not found: 404 not found\n");       res.end();       return;     }      if (fs.statsync(filename).isdirectory()) {       filename += '/' + category + '/' + 'undo.png';     }      fs.readfile(filename, "binary", function (err, file) {       if (err) {         res.writehead(500, {           "content-type": "binary"         });         res.write(err + "\n");         res.end();         return;       }        res.writehead(200, {         "content-disposition": "attachment;filename=" + yourfilename,         'content-type': 'image/png',         'content-length': file.length       });       res.write(file);       res.end();     });   }); }); 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -