node realizes file download

1. Method 1:

router.post('/downloadPdf',(req,res)=>{
	// let { filename,mimeType } = req.body;
	let { filename,mimeType } = req.query;
	// var path="public/pdf/5ea56710fb5a2fdad7cabcf3.pdf";
	// filename = '02cbc5d9-e10f-4f8d-8fae-304cc609a8f0';  //pdf
	// filename = '6f3db1f9-e247-4aa5-bca7-93bae62a0079' // mp4 
	// filename = '6a908990-0c4f-4101-9e9d-4c8b00a8133b' // doc
	// filename='2055c73b-1b37-40bc-ba50-b6b8ef82e63c' // docx
	// mimeType='application/pdf';

	let type = getType(mimeType);  //Get file suffix
	console.log(filename,mimeType,type);

	// path = path.join(__dirname, '../public/showPdf/'+filename+'.'+type);
	path='./public/showPdf/'+filename+'.'+type;
	// path="C:\Users\sswc\Desktop\gai2\public\showPdf\02cbc5d9-e10f-4f8d-8fae-304cc609a8f0.pdf"
	console.log(path);


	var fileStream = fs.createReadStream(path);
	var size = fs.statSync(path).size;
	res.setHeader('Content-Type', mimeType);
	res.setHeader('Content-Disposition', `attachment;filename=${filename}.${type}`); 
	res.setHeader('Content-Length', size);
	fileStream.on('data', function (data) {
		res.write(data, 'binary');
	});
	fileStream.on('end', function () {
		res.end();
		console.log('The file has been downloaded successfully!');
	});
	
})

 2. Method 2:

router.post('/downloadPdf',(req,res)=>{
	// let { filename,mimeType } = req.body;
	let { filename,mimeType } = req.query;
	// var path="public/pdf/5ea56710fb5a2fdad7cabcf3.pdf";
	// filename = '02cbc5d9-e10f-4f8d-8fae-304cc609a8f0';  //pdf
	// filename = '6f3db1f9-e247-4aa5-bca7-93bae62a0079' // mp4 
	// filename = '6a908990-0c4f-4101-9e9d-4c8b00a8133b' // doc
	// filename='2055c73b-1b37-40bc-ba50-b6b8ef82e63c' // docx
	// mimeType='application/pdf';

	let type = getType(mimeType);  //Get file suffix
	console.log(filename,mimeType,type);

	// path = path.join(__dirname, '../public/showPdf/'+filename+'.'+type);
	path='./public/showPdf/'+filename+'.'+type;
	// path="C:\Users\sswc\Desktop\gai2\public\showPdf\02cbc5d9-e10f-4f8d-8fae-304cc609a8f0.pdf"
	console.log(path);


	fs.readFile(path, (err, data) => {
		if (err) {
		  throw err
		}
		res.setHeader('Content-Type',mimeType)
		// Set file name
		// Content disposition is an extension of the MIME Protocol, which instructs the mime user agent how to display attached files
		// Attachment is downloaded as an attachment
		res.setHeader('Content-Disposition', `attachment; filename=${filename}.${type}`)
		// Return data
		res.end(data)
	})

})

For the above two methods of downloading code through node, the file path needs to be obtained through the relative path, and the path provided by node cannot be obtained, as shown below:

path = path.join(__dirname, '../public/showPdf/'+filename+'.'+type);

When I first download a file, I get the path of the file through path. The file can be downloaded, but it can only be downloaded once. When I download the second time, there will be a problem when I get the path through path, resulting in an error in the interface package 500. At that time, I was delayed for a whole day and didn't find the fault, but the download interface I wrote can only call one side. Later, I looked at what was different from what others wrote. Suddenly, I saw that it was because of the path. When my path is obtained through the relative path, the download interface returns to normal.  

Note: the getType() method here is used to obtain the suffix name of the file, because there are many forms of files supported by the platform when we did it. In order to download various files and find the corresponding files better, we defined a method to be compatible with various types of files. The method is as follows:

// Gets the suffix of the file
function getType(mimeType){
	switch(mimeType) {
		case 'video/x-msvideo':  return 'avi';
		case 'video/x-ms-wmv': return 'wmv';
		case 'video/x-flv': return 'flv';
		case 'image/x-ico': return 'ico';
		case 'image/x-psd': return 'psd';
		case 'text/csv': return 'csv';
		case 'text/html': return 'html';
		case 'text/plain': return 'txt';
		case 'text/xml': return 'xml';
		case 'video/mp4': return 'mp4';
		case 'video/mpeg': return 'mpg';
		case 'application/x-shockwave-flash': return 'swf';
		case 'audio/mpeg': return 'mp3';
		case 'audio/x-ogg': return 'ogg';
		case 'audio/x-wav': return 'wav';
		case 'image/jpeg': return 'jpg';
		case 'image/png': return 'png';
		case 'image/svg+xml': return 'svg';
		case 'application/x-mspublisher': return 'pub';
		case 'application/vnd.visio': return 'vsd';
		case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': return 'docx';
		case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': return 'xlsx';
		case 'application/vnd.openxmlformats-officedocument.presentationml.presentation': return 'pptx';
		case 'application/msword': return 'doc';
		case 'application/pdf': return 'pdf';
		case 'application/postscript': return 'ps';
		case 'application/rtf': return 'rtf';
		case 'application/vnd.ms-access': return 'mdb';
		case 'application/vnd.ms-excel': return 'xls';
		case 'application/vnd.ms-powerpoint': return 'ppt';
		case 'application/vnd.oasis.opendocument.text': return 'ott';
	}
}

  

  

Posted by miltonos on Wed, 11 May 2022 21:20:11 +0300