首页  >  文章  >  web前端  >  node.js如何实现简单的图片上传代码详解

node.js如何实现简单的图片上传代码详解

伊谢尔伦
伊谢尔伦原创
2017-07-24 13:09:251371浏览

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