首頁 >web前端 >js教程 >node.js如何實作簡單的圖片上傳程式碼詳解

node.js如何實作簡單的圖片上傳程式碼詳解

伊谢尔伦
伊谢尔伦原創
2017-07-24 13:09:251403瀏覽

1.node-formidable

對檔案上傳提供幫助的元件

2.app.js


var formidable = require('formidable');
var http = require( 'http' );
var sys = require('sys');
 
http.createServer(function( request ,response ){
  if( request.url == '/upload' && request.method.toLowerCase() == 'post' )
  {
    console.log( 'upload requet ' )
    uploadRequest(request,response);
    return;
  }
  enterRequest(request,response)
}).listen(3000);
 
function enterRequest( request, response )
{
  response.writeHead( 200, { 'Content-type' : 'text/html' });
  response.end(
    &#39;<form action = "/upload" enctype="multipart/form-data" method="post" >&#39; +
    &#39;<input type = "text" name = "title" /> <br>&#39; +
    &#39;<input type = "file" name="upload" multiple="multiple"/> <br/>&#39;+
    &#39;<input type="submit" value="Upload Now"/>&#39; +
    &#39;</form>&#39;
  );
}
 
/**
 * 处理上传的逻辑
 * @param request
 * @param response
 */
function uploadRequest( request,response )
{
  var form = new formidable.IncomingForm();
  form.parse( request, function ( err, fields, files ) {
    response.writeHead(200, {&#39;Content-type&#39; : &#39;text/plain&#39;});
    response.write(&#39;reviced upload file&#39;);
    response.end( sys.inspect(
      {
        fields : fields,
        files : files
      }) );
  });
}

#

以上是node.js如何實作簡單的圖片上傳程式碼詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn