English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Node.js File Upload Processing Example

直接主题,在Node.js web 开发基本框架的前提下,我们来做一个文件上传功能

上传的handler比较简单,网上都能找到

var url=require('url');
var exec=require('child_process').exec;
var querystring=require('querystring');
/********************************文件上传 第3模块测试*************************/
function fileUploadForm(request,response){
 response.writeHead(200,{'Content-Type':'text/html'});
 var body = '<html>'+
  '<head>'+
  '<meta http-equiv="Content-Type" '+
  'content="text/html; charset=UTF-8" /'>+
  '</head>'+
  '<body>'+
  '<form action="/fileuploadaction" method="post" enctype="multipart/form-data">'+
  '<input name="name" type="text" /'>+
  '<input name="upload" type="file" /'>+
  '<input type="submit" value="Upload" /'>+
  '</form>'+
  '</body>'+
  '</html>';
 response.write(body);
 response.end();
}
<span style="color: rgb(255function fileUploadAction(request,response){
 var fs=require('fs');
 var formidable=require('formidable');
 var baseUploadPath="./media/upload/";
 var form=new formidable.IncomingForm();
 form.uploadDir='./var/tmp';
 form.parse(request,function(error,fields,files){
  if(!error){
   console.log(fields);
   var desUploadName=baseUploadPath+files.upload.name;
   fs.renameSync(files.upload.path, desUploadName);
   response.writeHead(200,{'Content-Type':'text/html'});//It is worth noting that the content of the response.writeHead() function must be written in the callback of form.parse() otherwise it will not be displayed
   response.write('received image:</br>');
   response.write('<img src="/showuploadimage?name='+files.upload.name+" />');
   response.end();
  }
 });
</span>
function showUploadImage(request,response){
 var fs=require('fs');
 var imageName=querystring.parse(url.parse(request.url).query);
 var baseUploadPath="./media/upload/";
 fs.readFile(baseUploadPath+imageName.name, "binary", function(error, file) {
  if(error) {
   response.writeHead(500, {"Content-Type": "text/plain");
   response.write(error + "\n");
   response.end();
  } else {
   response.writeHead(200, {"Content-Type": "image/png");
   response.write(file, "binary");
   response.end();
  }
 });
}
exports.fileuploadform=fileUploadForm;
exports.fileuploadaction=fileUploadAction;
exports.showuploadimage=showUploadImage;

Also add in index.js

handle['/fileuploadform=handlers.fileuploadform; 
handle['/fileuploadaction=handlers.fileuploadaction; 
handle['/showuploadimage=handlers.showuploadimage; 

Something to note is that when handling file uploads, you cannot add

request.setEncoding('utf8');//Setting this may likely cause upload failure, this seems to be a bug in the formidable module 

and

request.addListener("data",function(tempPostData){ 
   postData+=tempPostData; 
  }); 
  request.addListener("end",function(){ 
   route(request,response,postData,handle); 
  }); 

That's all for this article, I hope it will be helpful to everyone's learning, and I also hope everyone will support and cheer for the tutorial.

Statement: The content of this article is from the network, the copyright belongs to the original author, the content is contributed and uploaded by Internet users, this website does not own the copyright, has not been manually edited, and does not bear relevant legal liability. If you find any suspected copyright content, please send an email to: notice#w3Please report violations by sending an email to codebox.com (replace # with @ when sending email), and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.

You May Also Like