Comments (8)
Nothing special, this is my solution:
var f = new formData()
f.append('uploadfile', fs.createReadStream(file))
f.append('formId', 1)
f.getLength(function(err, length) {
var token = qs.parse(url.parse(body.ErrMsg).query).token
request.post({
url: '/path/'
, headers: {
'content-length': length // 26xxxx
}
}, function(err, res, body) {
var formId = body.match(/formId, '(\d+)'/)
cb(err, formId && formId[1])
})._form = f
})
Here goes getLengthSync()
version(wrong length)
var f = new formData()
f.append('uploadfile', fs.createReadStream(file))
f.append('formId', 1)
request.post({
url: wx('/cgi-bin/uploadmaterial?cgi=uploadmaterial&type=0&t=iframe-uploadfile&lang=zh_CN&formId=1', token)
, headers: {
'content-length': f.getLengthSync() // 214
}
}, function(err, res, body) {
var formId = body.match(/formId, '(\d+)'/)
cb(err, formId && formId[1])
})._form = f
from form-data.
So I checked at this request/request#396, seems this should be implemented by my own outside this library.
But still, getLengthSync()
did not return the actual body length as I expected.
from form-data.
Can you show me piece of your code where you prepare data to send to the server and where you do submit? Thank you.
from form-data.
thank you @yyfrankyy, The same problem when uploading a file to my CMS on mochiweb server.
from form-data.
I'm not sure this is a bug, but content-length should be prepared in headers so that server knows when the chunk datas end.
No, it's not a bug. See:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1
All HTTP/1.1 applications MUST be able to receive and decode the "chunked" transfer-coding, and MUST ignore chunk-extension extensions they do not understand.
That being said, I'm not against a patch that tries to determine and set the Content-Length
for well known fixed size streams such as files. (However, this has to be done with great care and documentation - e.g. what happens if a file changes in size while uploading).
from form-data.
@felixge I think that should be documented with a sample. Content-Length can be set for certain number of cases by form-data, and that would be more reliable and save many other's time!
from form-data.
This is what we have in Readme:
Form-Data can recognize and fetch all the required information from common types of streams (fs.readStream, http.response and mikeal's request), for some other types of streams you'd need to provide "file"-related information manually, followed by example.
Although, I tested that behavior quite a bit and so far only node.js based server can accept chunked file uploads,
there are patches for couple Ruby standalone web server, but I haven't tried them yet.
So it's not a bug which collides with most implementations because they share common bug.
from form-data.
@yyfrankyy I updated Readme.
from form-data.
Related Issues (20)
- Published Packages Contain unwanted files HOT 3
- Doesn't play nice with node v18 undici fetch HOT 3
- getHeaders should add content length if known HOT 1
- Passing Uint8Array or Buffer leads to "source.on is not a function" error under Node 18 HOT 1
- Need Help Browser support HOT 3
- Final nail in the coffin HOT 1
- Is there a place to report security issues? HOT 2
- From Data HOT 1
- Is this Library Dead? HOT 1
- "form-data" is vulnerable to request tampering via a crafted filename HOT 1
- FormData is undefind
- FormData is undefind
- FormData class is confused with native FormData class in Chrome HOT 1
- Doesn't work well in node version 18 HOT 3
- any option to Read all the key and value from formdata HOT 1
- How to send an array of pdf files using form-data HOT 5
- How do I get upload progress when I submit??? HOT 1
- Mark package with sideEffects: false
- how to upload multiple files with fields at once with formdata
- Is it possible to cancel a fileupload?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from form-data.