Home > Web Front-end > CSS Tutorial > Simple implementation of node.js image upload

Simple implementation of node.js image upload

高洛峰
Release: 2016-12-24 17:30:44
Original
1610 people have browsed it

The example in this article shares the specific code for node.js image uploading for your reference. The specific content is as follows

1.node-formidable

Component that helps file upload

Simple implementation of node.js image upload

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
      }) );
  });
}
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s learning. I also hope that everyone will support the PHP Chinese website.


For more articles related to the simple implementation of node.js image uploading, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template