2017-09-08 127 views
0

這裏有什麼問題?節點js文件上傳出錯

我有文件節點js文件上傳動作和另一個HTML文件

文件到upload.js


var formidable = require('formidable'); 

var http = require('http'); 

var form = new formidable.IncomingForm(); 

http.createServer(function(req, res){ 

    form.parse(req, function(err, fields, files){ 
     console.log(files.filetoUpload.path); 
    }); 
}).listen(3002); 

fileUpload.html

<body> 
    <form action="" enctype="multipart/form-data" method="post"> 

     <input type="file" name="filetoUpload"> 
     <input type ="submit" value="Upload"> 
    </form>  
</body> 

Exception has occurred: Error TypeError: Cannot read property 'path' of undefined at d:\CUBIC\UI\asg\1\FileUpload.js:9:39 at IncomingForm. (d:\CUBIC\UI\asg\1\node_modules\formidable\lib\incoming_form.js:105:9) at emitNone (events.js:86:13) at IncomingForm.emit (events.js:185:7) at IncomingForm._maybeEnd (d:\CUBIC\UI\asg\1\node_modules\formidable\lib\incoming_form.js:553:8) at Object.end (d:\CUBIC\UI\asg\1\node_modules\formidable\lib\incoming_form.js:239:12) at IncomingMessage. (d:\CUBIC\UI\asg\1\node_modules\formidable\lib\incoming_form.js:130:30) at emitNone (events.js:86:13) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:974:12)

+0

當你記錄'files'時你會得到什麼? – turmuka

+0

錯誤消息試圖告訴你'files.filetoUpload'的屬性'path'沒有定義。也許'err'被填充?你有沒有檢查它? –

+0

在files.filetoUpload.path中包含fakepath示例C:\\ Users \\ SYEDAY〜1 \\ AppData \\ Local \\ Temp \\,它不存在於system.So中,這就是爲什麼它顯示爲undefined。 –

回答

0

它應該是files.filetoupload.path),它似乎已經錯誤地編碼爲,files.filetoUpload.path)(以U大寫)。

希望這會有所幫助。

0

According to the specifications of HTML5, a file upload control should not reveal the real local path to the file you have selected, if you manipulate its value string with JavaScript. Instead, the string that is returned by the script, which handles the file information is C:\fakepath.

This requirement is already implemented in Internet Explorer 8 - the real path to the file will be shown only if the page that contains the control is added to the trusted sites collection of the browser.

如果你想更換假的路徑使用這樣

這是有意義的;本質上是瀏覽器餵養跛C:在\ fakepath \文本幸運的是所有我需要做的是做一個簡單的字符串替換呼叫解決這個問題:

// Change the node's value by removing the fake path 
inputNode.value = fileInput.value.replace("C:\\fakepath\\", ""); 

需要導入路徑模塊中上傳。 js文件。嘗試在你的代碼var path=require('path');使用該安裝使用npm install path

試試這個代碼路徑模塊,你可以看到這兩個瀏覽器的響應,並在終端響應。

var multiparty = require('multiparty'); 
var http = require('http'); 
var util = require('util'); 
var fs = require('fs'); 
var path = require('path'); 
http.createServer(function(req, res) { 
    if (req.url === '/upload' && req.method === 'POST') { 
    // parse a file upload 
    var form = new multiparty.Form(); 

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

     var key=files.upload[0]; 
     fs.readFileSync(key.path); 
     console.log("path":key.path); 
     console.log("File name":key.originalFilename); 
     res.writeHead(200, {'content-type': 'text/plain'}); 
     res.write('received upload:\n\n'); 
     res.end(util.inspect({fields: fields ,files:files.upload})); 
    }); 

    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" id="file-id"><br>'+ 
    '<input type="submit" value="Upload">'+ 
    '</form>' 
); 
}).listen(8080); 

運行服務器node app.js並上傳使用http://localhost:8080希望這有助於該文件。