Monday, 29 May 2017

File upload with pure nodejs

i am trying to upload a file with pure nodejs

      var req = getHttpRequestInstance(function afterRequestSend(req,res){
        // THIS BLOCK NEVER CALLED. WHY ?
      });
      const mo = options.multipartOptions;
      if (!(mo.boundaryKey)) {
        mo.boundaryKey = Math.random().toString(16);
      }
      req.setHeader('Content-Type', 'multipart/form-data; boundary="'+mo.boundaryKey+'"');
      if(mo.contentLength){
        req.setHeader('Content-Length', mo.contentLength);
      }
      if (isObject(mo.formData)) {
        for(let formKey in mo.formData){
          req.write(
            '--' + mo.boundaryKey + '\r\n'
            + 'Content-Disposition: form-data; name="'+formKey+'"\r\n'
            + mo.formData[formKey] + '\r\n'
          );
        }
      }
      req.write(
        '--' + mo.boundaryKey + '\r\n'
        + 'Content-Type: '+ (mo.mimeType || 'application/octet-stream') +'\r\n'
        + 'Content-Disposition: form-data; name="'+ (mo.fieldName || 'file1') +'"; filename="'+ (mo.fileName || 'filename') +'"\r\n'
      );
      options.payloadStream.pipe(req, { end : false });
      options.payloadStream.once('end',req.end.bind(req,'\r\n--' + mo.boundaryKey + '--'));
      options.payloadStream.once('error',reject);

And its never ending process after then

HTTP 17278: sockets service.com:443::::::::: 2
HTTP 17278: write ret = false
HTTP 17278: write ret = false
HTTP 17278: write ret = false
HTTP 17278: write ret = false
HTTP 17278: CLIENT socket onClose
HTTP 17278: removeSocket service.com:443::::::::: writable: false
HTTP 17278: HTTP socket close
HTTP 17278: write ret = true
HTTP 17278: outgoing message end.

The after request callback is never called. Any help?

PS : I am not willing to use any lib. Just pure nodejs.



via codeofnode

No comments:

Post a Comment