Nodejs File Upload

Source: Internet
Author: User
Tags readfile

Method One:---------support Chinese, picture display------------
//-------File upload------
router.post ('/imgupload ', function (req,res) {
//-----------------------------------------
var form = new multiparty. Form ();
//settings edit
form.encoding = ' utf-8 ';
//Set File Store path
Form.uploaddir = "./uploads/";
//Set Single file size limit
Form.maxfilessize = 2 * 1024 * 1024;
//form.maxfields = 1000; set the sum of the size of the file

Form.parse (req, Function (Err, fields, files) {

for (var key in fi Les) {
Console.log (key+ ' = = ' +files[key]);
for (var kk in files[key][0].headers) {
Console.log (kk+ ' = = = ' +files[key][0].headers[kk]);
}
}
File1 = files[' myimg '];
Paraname = File1[0].fieldname; Parameter name myimg
OriginalFilename = file1[0].originalfilename;//original file name
Tmppath = file1[0].path;//uploads\ Mrecqcv2cglzbj-umjnyw_bz.txt
FileSize = file1[0].size;//File size

var timestamp=new Date (). GetTime (); Gets the current timestamp
newpath= './public/images/' +timestamp+originalfilename;

var filereadstream = Fs.createreadstream (Tmppath);
var filewritestream = Fs.createwritestream (NewPath);
Filereadstream.pipe (Filewritestream);//Pipe Flow
Filewritestream.on (' Close ', function () {
Console.log (' Copy Over ');
});
/*
function WriteFile (data) {
Fs.writefile (newpath,data,function (Error) {
if (error) {
throw error;
} else{
Console.log ("File Saved");
}
});
}
Fs.readfile (Tmppath, ' ASCII ', function (err, data) {
if (err) {
Console.log ("read failed");
} else {
WriteFile (data);
}
});
*/

//Rename to Real file name
/*
Fs.rename (tmppath, NewPath, function (err) {
if (err) {
Console.log (' Rename Error: ' + err);
} else {
Console.log (' rename ok ');
}
});
*/
//delete files in Temp folder
//fs.unlinksync (Tmppath);

});
//-----------------------------------------
Res.send (' upload complete ');
});

Method Two:
//----------have a Chinese problem, the picture does not appear-----------
//-------File upload------
router.post ('/imgupload ', function ( Req,res) {
//-----------------------------------------
var form = new multiparty. Form ();
//settings edit
form.encoding = ' utf-8 ';
//Set File Store path
Form.uploaddir = "./uploads/";
//Set Single file size limit
Form.maxfilessize = 2 * 1024 * 1024;
//form.maxfields = 1000; set the sum of the size of the file

Form.parse (req, Function (Err, fields, files) {

for (var key in fi Les) {
Console.log (key+ ' = = ' +files[key]);
for (var kk in files[key][0].headers) {
Console.log (kk+ ' = = = ' +files[key][0].headers[kk]);
}
}
File1 = files[' myimg '];
Paraname = File1[0].fieldname; Parameter name myimg
OriginalFilename = file1[0].originalfilename;//original file name
Tmppath = file1[0].path;//uploads\ Mrecqcv2cglzbj-umjnyw_bz.txt
FileSize = file1[0].size;//File size

var timestamp=new Date (). GetTime (); Gets the current timestamp
newpath= './public/images/' +timestamp+originalfilename;

function WriteFile (data) {
Fs.writefile (newpath,data,function (Error) {
if (Error) {
Throw error;
}else{
Console.log ("File Saved");
}
});
}
Fs.readfile (Tmppath, ' ASCII ', function (err, data) {
if (err) {
Console.log ("read failed");
} else {
WriteFile (data);
}
});

Rename to Real file name
/*
Fs.rename (Tmppath, NewPath, function (err) {
if (err) {
Console.log (' Rename error: ' + err);
} else {
Console.log (' rename ok ');
}
});
*/
Delete files in the Temp folder
Fs.unlinksync (Tmppath);

});
//-----------------------------------------
Res.send (' upload complete ');
});

Nodejs File Upload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.