node-modules / formstream Goto Github PK
View Code? Open in Web Editor NEWmultipart/form-data encoded stream, helper for file upload.
multipart/form-data encoded stream, helper for file upload.
npm i formstream
check 'formstream.js' (Line 134-137)
FormStream.prototype.field = function (name, value) {
var buffer = new Buffer(value);
return this.buffer(name, buffer);
};
Maybe some one want to send file content buffer directly.
安装依赖会安装2.0版本,因为node-mime代码方法名改了
Version 2 is a breaking change from 1.x as the semver implies. Specifically:
lookup() renamed to getType()
extension() renamed to getExtension()
charset() and load() methods have been removed
If you prefer the legacy version of this module please npm install mime@^1. Version 1 docs may be found here.
这样会引发报错
nodejs.TypeError: mime.lookup is not a function
请更新下指定版本
代码:
const formstream = require('formstream')
const fs = require('fs')
const path = require('path')
const filepath = path.join(__dirname, '1.jpg')
const file = fs.createReadStream(filepath)
const stream = formstream()
stream.file('uri', file)
stream.field('hello', 'world')
console.log(stream)
报错:
var ext = path.replace(/^.*[\.\/\\]/, '').toLowerCase();
^
TypeError: path.replace is not a function
at Mime.lookup (/project/node_modules/mime/mime.js:70:18)
at FormStream.file (/project/node_modules/formstream/lib/formstream.js:113:23)
at Object.<anonymous> (/project/form-data.js:35:8)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
I think formstream must be a Readable
const form = new FormStream();
form.field('foo', 'bar');
怎么在form里塞入请求里的流
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.