我正在學習「The Node初學者書」並在書中做練習,練習是呈現一張由用戶上傳的圖片。這是一個例子與node-formidable
寫道,代碼如下:使用節點強大的上傳圖片錯誤
var formidable = require('formidable'),
http = require('http'),
util = require('util');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}).listen(8888);
我node filename.js
運行它,然後我打開我的瀏覽器位於http://localhost:8888/upload,當屬以下東西:
我進入名稱並選擇一個文件,然後說到如下:
我點擊upload
按鈕,如下回應:
received upload:
{ fields: { title: 'Hello Wolrd' },
files:
{ upload:
File {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
size: 37417,
path: '/tmp/upload_a306115e1e630a0c548b6d820fe803cb',
name: 'myfile_icon_file_4.png',
type: 'image/png',
hash: null,
lastModifiedDate: 2016-10-11T03:52:41.052Z,
_writeStream: [Object] } } }
如何獲得財產path
?爲什麼在這裏創建一個單詞File
?
這是'強大'庫的內部表示。 「文件」的一個實例。你可以通過以下途徑訪問路徑:'files.upload.path' – notion
我試過了,但是出現錯誤:'TypeError:無法讀取未定義的屬性'路徑',如何? – Rico