superagent upload
See original GitHub issueimport 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:
- Created 6 years ago
- Comments:9 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We don’t support piping of uploads. You must use
.attach()
to upload a file.The code throws a
write after end
error: