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