Home Web Front-end JS Tutorial Node.js simulation browser file upload example_javascript skills

Node.js simulation browser file upload example_javascript skills

May 16, 2016 pm 04:54 PM
node.js File Upload simulation Browser

I posted it on OSChina, that one is mine too, now I put it here, haha ​​

This code can only upload one file at a time~~

Copy the code The 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',
' .jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.js': 'application/json',
'.torrent': 'application/octet -stream'
};
var ext = path.extname(filename);
var mime = mimes[ext];
mime=!!mime?mime:'application/octet-stream' ;
return mime;
}
//Get the bounds check random string
var getBoundary=function() {
var max = 9007199254740992;
var dec = Math.random() * max;
var hex = dec.toString(36);
var boundary = hex;
return boundary;
}
//Get boundary
var getBoundaryBorder=function (boundary ) {
return '--' boundary 'rn';
}
//Field formatting
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);
} );
}

//Various settings
var opt={
"url":"http://xxxx.xx",//url
"file" :"00.jpg",//File location
"param":"file",//File upload field name
"field":{//Other 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--
*/

The last comment is the standard format" n" represents the actual string. In order to look comfortable, it is also visually adjusted.
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use gRPC to implement file upload in Golang? How to use gRPC to implement file upload in Golang? Jun 03, 2024 pm 04:54 PM

How to use gRPC to implement file upload in Golang?

How to set the browser to start automatically when booting? How to automatically open the browser when Win11 is started? How to set the browser to start automatically when booting? How to automatically open the browser when Win11 is started? Jun 12, 2024 pm 07:58 PM

How to set the browser to start automatically when booting? How to automatically open the browser when Win11 is started?

How to implement drag and drop file upload in Golang? How to implement drag and drop file upload in Golang? Jun 05, 2024 pm 12:48 PM

How to implement drag and drop file upload in Golang?

Sesame Open Door Official Website Sesame Open Door App Latest Entering Website Sesame Open Door Official Website Sesame Open Door App Latest Entering Website Feb 28, 2025 am 11:18 AM

Sesame Open Door Official Website Sesame Open Door App Latest Entering Website

Clear the Google Chrome registry and clean up the remaining residue from uninstalling Google Chrome Clear the Google Chrome registry and clean up the remaining residue from uninstalling Google Chrome Jun 19, 2024 am 11:09 AM

Clear the Google Chrome registry and clean up the remaining residue from uninstalling Google Chrome

The latest version of Sesame Open Door Official Website Entrance gate.io Official Website Link Address Entrance The latest version of Sesame Open Door Official Website Entrance gate.io Official Website Link Address Entrance Feb 28, 2025 am 11:21 AM

The latest version of Sesame Open Door Official Website Entrance gate.io Official Website Link Address Entrance

gate.io sesame door download Chinese tutorial gate.io sesame door download Chinese tutorial Feb 28, 2025 am 10:54 AM

gate.io sesame door download Chinese tutorial

Comparison of Golang and Node.js in backend development Comparison of Golang and Node.js in backend development Jun 03, 2024 pm 02:31 PM

Comparison of Golang and Node.js in backend development

See all articles