Giter VIP home page Giter VIP logo

Comments (8)

yyfrankyy avatar yyfrankyy commented on May 28, 2024 1

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.

yyfrankyy avatar yyfrankyy commented on May 28, 2024

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.

alexindigo avatar alexindigo commented on May 28, 2024

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.

behrad avatar behrad commented on May 28, 2024

thank you @yyfrankyy, The same problem when uploading a file to my CMS on mochiweb server.

from form-data.

felixge avatar felixge commented on May 28, 2024

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.

behrad avatar behrad commented on May 28, 2024

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

alexindigo avatar alexindigo commented on May 28, 2024

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.

alexindigo avatar alexindigo commented on May 28, 2024

@yyfrankyy I updated Readme.

from form-data.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.