question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

superagent upload

See original GitHub issue
import convert from 'koa-convert';
import request from 'superagent';
const promisePipe = require('promisepipe');
import config from '../../config';
const fs = require('fs');
const is = require('is-type-of');
const parse = require('co-busboy');


const put = function* put(name, file) {
  let content;

  try {
 if (is.readableStream(file)) {
      console.log('is readableStream==');
      const req = request.post('http://127.0.0.1:8081/rule/testUpload').set('Transfer-Encoding', 'chunked').timeout(5000);
      yield file.pipe(req, { end: false }).on('error', (err) => {
        console.log('file.pipe error===========>', err);
      }).on('end', (err, res) => {
        console.log('file.pipe end err===========>', err);
        console.log('file.pipe end res===========>', res);
      });
    } else {
      throw new TypeError('Must provide String/Buffer/ReadableStream for put.');
    }
    console.log('return==');
  } catch (e) {
    console.log(e);
  }
};


const uploadFile = convert(function* (next) {
  console.log('next============>', next);
  console.log('this============>', this);
  // the body isn't multipart, so busboy can't parse it
  if (!this.request.is('multipart/*')) return yield next;

  const parts = parse(this);
  let part;
  let aliyunOss;
  try {
    while (part = yield parts) {
      console.log('file part', part);
      if (part && part.filename) {
        const res = yield put(part.filename, part);
        console.log('yield put res=========>', res);
      }
    }
  } catch (e) {
    console.log(e);
  }

想用ant-design + superagent上传文件 新手,前端大神帮看下 报异常

Error: write after end at ClientRequest.OutgoingMessage.write (_http_outgoing.js:439:15) at Request.write (D:\react\phoenix20170309_11934_1\node_modules\superagent\lib\node\index.js:331:14) at FileStream.ondata (_stream_readable.js:555:20) at emitOne (events.js:96:13) at FileStream.emit (events.js:188:7) at FileStream.Readable.read (_stream_readable.js:381:10) at flow (stream_readable.js:761:34) at resume (_stream_readable.js:743:3) at _combinedTickCallback (internal/process/next_tick.js:74:11) at process._tickCallback (internal/process/next_tick.js:98:9)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kornelskicommented, Jun 13, 2017

We don’t support piping of uploads. You must use .attach() to upload a file.

0reactions
ian-speerscommented, Dec 24, 2018

The code throws a write after end error:

const superagent = require('superagent');
const fs = require('fs');

const inStream = fs.createReadStream('in.json');
const outStream = fs.createWriteStream('out.json');
const request = superagent.post('https://postman-echo.com/post');

request.type('json');
inStream.pipe(request).pipe(outStream);
Error: write after end
    at write_ (_http_outgoing.js:622:15)
    at ClientRequest.write (_http_outgoing.js:617:10)
    at Request.write (.../node_modules/superagent/lib/node/index.js:384:14)
    at ReadStream.ondata (_stream_readable.js:639:20)
    at emitOne (events.js:116:13)
    at ReadStream.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at ReadStream.Readable.push (_stream_readable.js:208:10)
    at fs.read (fs.js:2058:12)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to send files with superagent - ajax - Stack Overflow
I've just used "attach" to upload files here on one of my projects. The theoretical implementation is very well explained here: abandon.ie/notebook/simple-file- ...
Read more >
attach didn't upload file · Issue #1444 · ladjs/superagent - GitHub
We try to use superagent to upload file to our server by following this guide, https://visionmedia.github.io/superagent/#multipart-requests, ...
Read more >
Uploading files with superagent in the browser - Gaya Kessler
This article explains how to send multipart upload requests using superagent in the browser.
Read more >
superagent - npm
Small progressive client-side HTTP request library, and Node.js module with the same API, supporting many high-level HTTP client features.
Read more >
Image Upload Code Sample - JavaScript with Superagent
Image Upload Code Sample - JavaScript with Superagent The code below is using the superagent library to simplify handling of the HTTP request....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found