2017-09-08 14 views
0

ここで問題は何ですか?アップロードするノード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> 
をupload.jsする

ファイルのアクションをアップロードするファイルのノードのjsファイルを持っているし、別のHTMLファイル

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

'ファイル 'をログに記録するとどうなりますか? – turmuka

+0

エラーメッセージは、 'files.filetoUpload'の' path'プロパティが定義されていないことを伝えようとしています。たぶん 'err'にデータが入っているのでしょうか?あなたはまだそれをチェックしていますか? –

+0

files.filetoUpload.pathには、fakepathサンプルが含まれています。C:\\ Users \\ SYEDAY〜1 \\ AppData \\ Local \\ Temp \\これはsystem.Soには存在しません。 –

答えて

0

それはfiles.filetoupload.path)であるべきであり、あなたが(大文字Uで)files.filetoUpload.path)、として誤ってこれをコード化されていましたようです。

これが役に立ちます。

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.

あなたが意味を成していた。この

のような偽のパスの使用を置き換えたい場合は、中\ fakepathの\テキスト幸いにも私が行うために必要なすべてが単純な文字列は、呼び出しを置き換えることによって、問題を修正した:基本的にブラウザがそのラメCを供給している。あなたは、あなたのアップロードにパスモジュールを必要とする必要が

// 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希望このことができますを使用してファイルをアップロードします。

関連する問題