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~~
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 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.

Vercel是什么?本篇文章带大家了解一下Vercel,并介绍一下在Vercel中部署 Node 服务的方法,希望对大家有所帮助!

gm是基于node.js的图片处理插件,它封装了图片处理工具GraphicsMagick(GM)和ImageMagick(IM),可使用spawn的方式调用。gm插件不是node默认安装的,需执行“npm install gm -S”进行安装才可使用。

如何用pkg打包nodejs可执行文件?下面本篇文章给大家介绍一下使用pkg将Node.js项目打包为可执行文件的方法,希望对大家有所帮助!

本篇文章带大家详解package.json和package-lock.json文件,希望对大家有所帮助!

本篇文章给大家分享一个Nodejs web框架:Fastify,简单介绍一下Fastify支持的特性、Fastify支持的插件以及Fastify的使用方法,希望对大家有所帮助!

node怎么爬取数据?下面本篇文章给大家分享一个node爬虫实例,聊聊利用node抓取小说章节的方法,希望对大家有所帮助!

本篇文章给大家分享一个Node实战,介绍一下使用Node.js和adb怎么开发一个手机备份小工具,希望对大家有所帮助!

先介绍node.js的安装,再介绍使用node.js构建一个简单的web服务器,最后通过一个简单的示例,演示网页与服务器之间的数据交互的实现。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
