クライアントから"socket.io"でzipファイルを送信して、javascriptの"formidable"モジュールを使って、送られてきたファイルの保存場所を"rename"を使って指定しました。
"rename"を使って newpath という名前で保存場所のパスに名前を付けました。
こちらの newpath を使って、"child_process"でのマルチプロセスで、linuxコマンドを叩きたいのですが、どうも上手く行きません。
formidableによるnewpathの指定が上手くいっていない様子です。
実際に実行してみても、zipファイルの解凍が出来ている様子はございません。
しかし、パスを指定せず現在のフォルダでは実行可能です。
err等も出てこないので、お手上げ状態です。
どなたか私を助けてください...
↓サーバーの立ち上げとsocket通信ファイル
js
1var app = require("http").createServer(handler), 2io = require("socket.io").listen(app), 3fs = require("fs") ; 4const file = require("./file.js"); 5 6app.listen(3000) ; 7console.log("server start!"); 8function handler(req,res){ 9 fs.readFile(__dirname+"/file.html",function(err,data){ 10 if (req.url == '/fileupload') { 11 file(req,res); 12 res.write('File uploaded and moved!'); 13 res.end(); 14 }else{ 15 res.writeHead(200); 16 res.write(data); 17 res.end(); 18 } 19 }); 20} 21io.sockets.on("connection",function(socket){ 22 console.log("connect") ; 23 socket.on("upload",function(data){ 24 console.log(data) ; 25 socket.emit("move",data); 26 }); 27}); 28
↓htmlでzipファイルを送信する
html
1<!DOCTYPE html> 2<html lang="ja" > 3 <head> 4 <meta charset="utf-8"> 5 <title>file,pass</title> 6 </head> 7 <body> 8 <form action="fileupload" method="post" enctype="multipart/form-data"> 9 <input type="file" name="filetoupload"><br> 10 <input type="submit"> 11 </form> 12 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 13 </script> 14 <script src="http://localhost:3000/socket.io/socket.io.js"></script> 15 16 17<script> 18function act(){ 19 var socket = io.connect(); 20 $("#uploadfile").submit(function(e){ 21 socket.emit("emit_data",e); 22 e.preventDefault();//submitの実行を止める 23 }); 24 socket.on("data_from_server",function(data){ 25 console.log(data); 26 }); 27}; 28act(); 29</script> 30 </body> 31</html> 32
↓"formidable"を使って送られてきたファイルをサーバーで扱えるようにしている。renameでzipファイルの保存場所を指定。
ここの保存場所がいけないのかも?
"./motegiyuuta/Desktop/"->"./"なら正常にファイルの解凍ができますが、指定したフォルダで処理したいです。
js
1var fs = require("fs"); 2var formidable = require("formidable") ; 3const ls = require("./childprocess"); 4module.exports=function file(req,res){ 5 var form = new formidable.IncomingForm(); 6 form.parse(req, function (err, fields, files) { 7 var oldpath=files.filetoupload.path ; 8 var newpath="./motegiyuuta/Desktop/"+files.filetoupload.name ; 9 fs.rename(oldpath,newpath,function(err){ 10 if(err) res.end(err) ; 11 zip(newpath); 12 console.log("file,move",newpath); 13 }); 14 }); 15 } 16
↓"child_process"を使って newpathに対して Linuxコマンドを使いたい。
js
1const {spawn} = require('child_process') ; 2const file = require("./file.js") ; 3var fs =require("fs"); 4 5module.exports = function zip(newpath){ 6var ls = spawn("unzip",[newpath]); 7 ls.stdout.on('data', function(data){ 8 console.log(`stdout: ${data}`); 9 }); 10}; 11
何卒よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。