2017-04-06 8 views
0

私はnodejsをはじめて試みています。私はPythonシェルで使用しています。私は私がPOSTリクエストからファイルを抽出するnodejs

f = open(filePath, 'rb') 
try: 
    response = requests.post(serverURL, data={'command':'savefile'}, files={os.path.basename(filePath): f}) 

(クライアントPC)からファイルを送信する別の使用してPOSTリクエスト

app.js(サーバーPC)

app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: false })); 

app.post('/mytestapp', function(req, res) { 
    console.log(req) 
    var command = req.body.command; 
    var parameter = req.body.parameter; 
    console.log(command + "|" + parameter) 
    pyshell.send(command + "|" + parameter); 
    res.send("POST Handler for /create") 
}); 

のpythonファイルに1台のPCからファイルを転送しようとしていますfiddlerを使用していますが、要求にはクライアントPC上のファイルが含まれているようですが、サーバーPC上でファイルを取得できないようです。どのようにしてファイルを抽出して保存できますか?私はヘッダーがないのでそれですか?私は何を使うべきですか?ありがとうございました

答えて

0

私はあなたの質問の構文に基づいてExpressを使用していると推測します。 Expressは、ファイルのアップロードのためのサポートを出荷時に同梱していません。

multerまたはbusboyミドルウェアパッケージを使用して、multipartアップロードサポートを追加できます。

そのこれを行うには、実際にはかなり簡単で、ここmulterでサンプル

const express = require('express') 
const bodyParser = require('body-parser') 
const multer = require('multer') 

const server = express() 
const port = process.env.PORT || 1337 

// Create a multer upload directory called 'tmp' within your __dirname 
const upload = multer({dest: 'tmp'}) 

server.use(bodyParser.json()) 
server.use(bodyParser.urlencoded({extended: true})) 

// For this route, use the upload.array() middleware function to 
// parse the multipart upload and add the files to a req.files array 
server.port('/mytestapp', upload.array('files') (req, res) => { 
    // req.files will now contain an array of files uploaded 
    console.log(req.files) 
}) 

server.listen(port,() => { 
    console.log(`Listening on ${port}`) 
}) 
+0

感謝です!クライアントPC(pythonスクリプト)から、ファイルをアップロードするコードは何ですか? – golu

+0

@goluは、アップロードされたファイルをNodejsサーバーにどのように扱うかという質問とは異なる質問ですか? – peteb

+0

はい、そうかもしれません。私はそれに対して別の質問が必要ですか?また、私はその部分に固執しています。 – golu

関連する問題