溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Node.js文件上傳代碼

發(fā)布時(shí)間:2020-07-19 04:33:10 來源:網(wǎng)絡(luò) 閱讀:459 作者:red_7799 欄目:web開發(fā)
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var fs = require("fs");
var multer  = require('multer');
var iconv = require('iconv-lite');
var filelist = require('./filelist');

var urlencodedParser = bodyParser.urlencoded({extended:false});
app.use(multer({ dest: '/tmp/'}).array('p_w_picpath'));


app.use(express.static('public'));
app.get('/',function(req,res){
	res.sendFile(__dirname + "/public/" + "h2.html");
});

app.get("/filelist",function(req,res){
    console.log("GET filelist");

    var txt = filelist.fileList('/books');
            
    res.writeHead(200,{'Content-Type':'text/html'});
    res.end(txt);
})

app.post("/process_post",urlencodedParser,function(req,res){
	var txt = "<!DOCTYPE html><html><head><title>這是一個(gè)測試頁面</title><meta charset=\"utf-8\"></head><body>姓名:"+
		req.body.first_name+"<br />年齡:"+
		req.body.age+"</body></html>";
	/*var response={
		"first_name":req.body.first_name,
		"age":req.body.age
	};*/
		//console.log(response);
		res.writeHead(200, {'Content-Type': 'text/html'});
		res.end(txt);
});

app.post('/file_upload', function (req, res) {
 
   console.log(req.files[0]);  // 上傳的文件信息
 
   var des_file = __dirname + "/books/" + req.files[0].originalname;
   fs.readFile( req.files[0].path, function (err, data) {
        fs.writeFile(des_file, data, function (err) {
         if( err ){
              console.log( err );
         }else{
               response = {
                   message:'File uploaded successfully', 
                   filename:req.files[0].originalname
              };
          }
          console.log( response );
          
          var txt = "<!DOCTYPE html><html><head><title>這是一個(gè)測試頁面</title><meta charset=\"utf-8\"></head><body>文件名:"+
          			response.filename+"</body></html>";
          res.writeHead(200, {'Content-Type': 'text/html'});
          res.end(txt);
          //res.end( iconv.encode(JSON.stringify( response.filename ) ,'utf-8').toString());
       });
   });
});

var server = app.listen(80,'localhost',function(){  //如果不指定主機(jī)名'localhost',默認(rèn)是 IPv6
	var host = server.address().address;
    var port = server.address().port;
    //console.log(host);

	console.log("訪問地址:http://%s:%s",host,port);
});

// 終端打印如下信息
console.log('Server running at http://127.0.0.1:80/');

相關(guān)的H1.html文件內(nèi)容:

<!DOCTYPE html>
<html>
<head>
<title>這是一個(gè)測試頁面</title>
<meta charset="utf-8">
</head>
<body>
<form  id="tform" action="/process_post" method="post">
	姓名:<input type="text" name="first_name"><br />
	年齡:<input type="text" name="age"><br />
	<input type="submit" value="提交">
</form>
<br />
<h4>文件上傳:</h4>
選擇一個(gè)文件上傳: <br />
<form action="/file_upload" method="post" enctype="multipart/form-data">
<input type="file" name="p_w_picpath" size="50" />
<br />
<input type="submit" value="上傳文件" />
</form>
<br />
<a href="/filelist">文件瀏覽</a>
</body>
</html>


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI