Node.js Simulation Browser File Upload Example

Source: Internet
Author: User
Tags file upload require

  This article mainly introduces the Node.js simulation browser file upload implementation code, the need for friends can refer to the following

Oschina, that's mine, now put it here, haha       code is as follows: Var path=require ("path");  var fs=require ("FS");  var Http=require ("http");   //post value payload  var getfield=function (field, value) {  return ' Content-disposition:form-data; Name= "' +field+ '" rnrn ' +value+ ' rn '; }   //File payload  var getfieldhead=function (field, filename) {   var filefieldhead= ' Content-disposition:form-data; Name= "' +field+ '"; Filename= "' +filename+ '" rn ' + ' Content-type: ' +getmime (filename) + ' rnrn ';  return filefieldhead; } / Get mime  var getmime=function (filename) {  var mimes = {  '. png ': ' Image/png ',  '. gif ': ' Image/gif ', &N Bsp '. jpg ': ' image/jpeg ',  '. jpeg ': ' Image/jpeg ',  '. js ': ' Appliction/json ',  '. Torrent ': ' application/ Octet-stream '  };  var ext = path.extname (filename);  var mime = mimes[ext];  mime=!! Mime?mime: ' Application/octet-stream ';  return mime; } //Get boundary check random string   var getboundary=function () {  var max = 9007199254740992;  var dec = math.random () * max;  var hex = dec.to String;  var boundary = hex;  return boundary; } //Get boundary  var getboundaryborder= function (boundary) {  return '--' +boundary+ ' rn '; } //Field format   function fieldpayload (opts) {  var payload=[];  for (Var ID in Opts.field) {  Payload.push (GetField (id,opts.field[id)); }  Payload.push ("");  return Payload.join (Getboundaryborder (opts.boundary)); }   //post data   function Postrequest (opts) {  Filereadstream (opts,function (buffer) {  var options=require (' url '). Parse ( Opts.url);  var header={};  var h=getboundaryborder (opts.boundary);  var e=fieldpayload (opts);  var a=getfieldhead (opts.param,opts.file);  var d= "rn" +h;  header["Content-length"]=buffer.bytelength (h+e +a+d) +buffer.length;  header["Content-type"]= ' Multipart/form-datA boundary= ' +opts.boundary;  options.headers=header;  options.method= ' POST ';  var req=http.request ( Options,function (res) {  var data= ';  res.on (' Data ', function (chunk) {  data+=chunk; });  Res.on (' End ', function () {  console.log (res.statuscode)   Console.log (data); }); });  Req.write (H+e+a); Log.diy (h+e+a+buffer+d);  req.write (buffer);  req.end (d); });   }  Read file   function Filereadstream (opts, FN) {  var readstream = fs.createreadstream (opts.file,{flags: ' R ', Encoding:null});  var chunks=[];  var length = 0;  readstream.on (' Data ', function (chunk) {  length = chunk.length;  Chunks.push (chunk); });  Readstream.on (' End ', function () {  var buffer = new Buffer ( Length);  for (var i = 0, pos = 0, size = Chunks.length i < size; i++) {  chunks[i].copy (buffer, POS);  POS + + chunks[i].length; }  fn (buffer); }); &nBsp }   //Various settings   var opt={  "url": "http://xxxx.xx",//url  "file": "00.jpg",//File location   "param": " file,//Files upload field name   "field": {//The remaining post fields   "Client": "1",  "title": "OK"  },  "boundary": "---- Webkitformboundary "+getboundary ()  }    postrequest (opt); /* ------ webkitformboundaryuzkmkaovuuysq1dtrn    Content-disposition:form-data; Name= "File"; Filename= "00.jpg" rn  content-type:application/octet-stream  rn  rn +filern   ------ webkitformboundaryuzkmkaovuuysq1dtrn    Content-disposition:form-data; Name= "FieldName"   rn  RN +valuern   ------webkitformboundaryuzkmkaovuuysq1dt--  */    Finally this annotation is the standard format "RN" represents the actual string, in order to look comfortable, also visually adjust the  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.