2016-07-15 2 views
0

私はnode.jsの初心者です。私もexpressを使用しています。Node.js、Express:クライアントでres.sendの値を処理するにはどうすればいいですか?

ファイルをサーバーにアップロードして保存しても問題がなければ、簡単なWebアプリケーションを作成して保存します。それは正常に動作しますが、今はクライアントに現在の状態(アップロードされているのか、それともファイルのサイズが大きいためうまくいかないのか)を伝えたいと思います。

res.send()を使用する必要がありますが、クライアントがファイルをアップロードした同じページ(すべての要素が「upload.html」)に表示したいと思います。私は、クライアント側のjavascriptを使って送信された情報を処理する必要があると思いますが、サーバーサイドのJavaScriptとクライアントサイドのJavaScriptとどうやって通信するのですか?または、クライアント側のjavascriptを使用する必要はありませんか?

(私はHTMLで、後でそれを組み合わせるしたいと思いますので、私はCSSを使用してサーバーからの応答を設計することができます。)

server.js:

var  express = require('express'), 
 
     fileUpload = require('express-fileupload'), 
 
     fs  = require('fs'), 
 
     obSizeOf = require('object-sizeof'), 
 
     app = express(); 
 

 
app.use(express.static("public")); 
 
app.use(fileUpload()); 
 

 
app.get("/upload.html", function(req, res){ 
 
     res.sendfile(__dirname + "/" +"upload.html"); 
 
}) 
 

 
app.post('/upload.html', function(req, res) { 
 
\t if(obSizeOf(req.files.sampleFile) > 10000000) 
 
     {  
 
       res.send("The size of the not-uploaded file is to large! Please use a file with a maximal size of 10MB"); 
 
       return; 
 
     } 
 
     else 
 
     { 
 
       var sampleFile;  
 
       if (req.files.sampleFile.name == "") { 
 
         res.send('No files were uploaded.'); 
 
         return; 
 
       } 
 
       else 
 
       { 
 
         sampleFile = req.files.sampleFile; 
 
         var typ = sampleFile.mimetype.split("/"); 
 
         console.log(typ[0]); 
 
         if(fs.existsSync("public/upload/image/"+typ[0]+"/"+sampleFile.name)) 
 
         { 
 
           res.send("A File with the same name already exists! Please rename it!"); 
 
           return;       
 
         } 
 
         else 
 
         { 
 
           sampleFile.mv('public/upload/'+typ[0]+'/'+sampleFile.name , function(err) { 
 
            if (err){ 
 
            res.send('File NOT UPLOADED!'); 
 
             } 
 
             else { console.log("Mieeep!"); res.send(typ[0].charAt(0).toUpperCase()+typ[0].slice(1) +' data uploaded!'); 
 
             } 
 
           }); 
 
         } 
 
       } 
 
     } 
 
}); 
 
app.listen("8000");

/upload.html:

<html> 
 
\t <body> 
 
\t \t <form ref='uploadForm' 
 
\t \t \t id='uploadForm' 
 
\t \t \t action='/upload.html' 
 
\t \t \t method='post' 
 
\t \t \t encType="multipart/form-data"> 
 
\t \t \t Upload File 
 
\t \t \t </br> 
 
\t \t \t \t <input type="file" name="sampleFile" /> 
 
\t \t \t </br> 
 
\t \t \t \t <input type='submit' value='Upload!' /> 
 
\t \t \t </br> 
 
\t \t \t <p id="serverInformation"></p> <!--Placeholder for information from the server--> 
 
\t \t \t Only images 
 
\t \t </form> \t \t 
 
\t </body> 
 
</html>

+0

あなたが送信するリアルタイムの情報を求める、あなたは[ウェブソケット](http://socket.io/)を使用する必要があります。 – DrakaSAN

答えて

0

実際に必要なのはソケットプログラミングです。ノードjsを使用すると、簡単にそれを行うことができます。

ソケットとノードjの詳細はthis linkを参照してください。

0

あなたはAJAXを使用してエラーステータスを確認できます。そこ

...
<script> 
$(document).ready(function() { 
$("#uploadForm").submit(function() { 

    $.ajax({ 
    type: "POST", 
    url: "/upload.html", 
    data: $(this).serialize(), 
    complete: function(xhr, statusText){ 
     alert(xhr.status+" : "+ statusText); 
    } 
    }) 
}) 
}) 
</script> 
関連する問題