nodejs 檔案上傳

來源:互聯網
上載者:User

標籤:syn   original   data   nbsp   party   sci   console   顯示   var   

方法一:---------支援中文,圖片顯示------------
//-------檔案上傳------
router.post(‘/imgupload‘,function(req,res){
//-----------------------------------------
var form = new multiparty.Form();
//設定編輯
form.encoding = ‘utf-8‘;
//設定檔案儲存體路徑
form.uploadDir = "./uploads/";
//設定單檔案大小限制
form.maxFilesSize = 2 * 1024 * 1024;
//form.maxFields = 1000; 設定所以檔案的大小總和

form.parse(req, function(err, fields, files) {

for(var key in files){
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; //參數名myimg
originalFilename = file1[0].originalFilename; //原始檔案名
tmpPath = file1[0].path;//uploads\mrecQCv2cGlZbj-UMjNyw_Bz.txt
fileSize = file1[0].size; //檔案大小

var timestamp=new Date().getTime(); //擷取目前時間戳
newPath= ‘./public/images/‘+timestamp+originalFilename;

var fileReadStream = fs.createReadStream(tmpPath);
var fileWriteStream = fs.createWriteStream(newPath);
fileReadStream.pipe(fileWriteStream); //管道流
fileWriteStream.on(‘close‘,function(){
console.log(‘copy over‘);
});
/*
function writeFile(data){
fs.writeFile(newPath,data,function(error){
if(error){
throw error;
}else{
console.log("檔案已儲存");
}
});
}
fs.readFile(tmpPath,‘ascii‘,function(err, data) {
if (err) {
console.log("讀取失敗");
} else {
writeFile(data);
}
});
*/

//重新命名為真實檔案名稱
/*
fs.rename(tmpPath, newPath, function(err) {
if(err){
console.log(‘rename error: ‘ + err);
} else {
console.log(‘rename ok‘);
}
});
*/
//刪除臨時檔案夾中的檔案
//fs.unlinkSync(tmpPath);

});
//-----------------------------------------
res.send(‘上傳完成‘);
});

方法二:
//----------有中文問題,圖片顯示不出來-----------
//-------檔案上傳------
router.post(‘/imgupload‘,function(req,res){
//-----------------------------------------
var form = new multiparty.Form();
//設定編輯
form.encoding = ‘utf-8‘;
//設定檔案儲存體路徑
form.uploadDir = "./uploads/";
//設定單檔案大小限制
form.maxFilesSize = 2 * 1024 * 1024;
//form.maxFields = 1000; 設定所以檔案的大小總和

form.parse(req, function(err, fields, files) {

for(var key in files){
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; //參數名myimg
originalFilename = file1[0].originalFilename; //原始檔案名
tmpPath = file1[0].path;//uploads\mrecQCv2cGlZbj-UMjNyw_Bz.txt
fileSize = file1[0].size; //檔案大小

var timestamp=new Date().getTime(); //擷取目前時間戳
newPath= ‘./public/images/‘+timestamp+originalFilename;

function writeFile(data){
fs.writeFile(newPath,data,function(error){
if(error){
throw error;
}else{
console.log("檔案已儲存");
}
});
}
fs.readFile(tmpPath,‘ascii‘,function(err, data) {
if (err) {
console.log("讀取失敗");
} else {
writeFile(data);
}
});

//重新命名為真實檔案名稱
/*
fs.rename(tmpPath, newPath, function(err) {
if(err){
console.log(‘rename error: ‘ + err);
} else {
console.log(‘rename ok‘);
}
});
*/
//刪除臨時檔案夾中的檔案
//fs.unlinkSync(tmpPath);

});
//-----------------------------------------
res.send(‘上傳完成‘);
});

 

nodejs 檔案上傳

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.