我正在尝试将包含文件的多部分表单数据从一个api发布到另一个。为此,我使用axios和一个FormData库来处理要发送的数据
import { ReadStream } from "fs"
import Axios from "axios"
import * as fs from "fs"
const FormData = require('form-data')
let formData: FormData = new FormData()
formData.append('name', name)
formData.append('allowPrint', 'false')
const file: ReadStream = fs.createReadStream(name)
formData.append('content', file, name)
return Axios.post(fileAPIUrl + "/file", formData, {
headers: {
"Origin": origin,
"Authorization": "Bearer " + securityToken,
"Content-Type": "multipart/form-data"
}, responseType: "json"
})
我应该收到一个json,其中包含我刚刚上传的文件的ID。我知道其他api可以正常工作,因为从前端开始工作,我可以从前端发布包含更多信息的文件,但是如果我从后端使用此网址,则会收到以下错误:
{ Error: write after end
at write_ (_http_outgoing.js:627:15)
at ClientRequest.write (_http_outgoing.js:622:10)
at RedirectableRequest._write node_modules/follow-redirects/index.js:153:23)
at doWrite (_stream_writable.js:406:12)
at clearBuffer (_stream_writable.js:533:7)
at onwrite (_stream_writable.js:458:7)
at onCorkedFinish (_stream_writable.js:657:5)
at afterWrite (_stream_writable.js:473:3)
at onwrite (_stream_writable.js:464:7)
at Socket._writeGeneric (net.js:742:5)
at Socket.connect (net.js:692:12)
at Object.onceWrapper (events.js:272:13)
at Socket.emit (events.js:185:15)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1157:10)
config:
{ adapter: [Function: httpAdapter],
transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
headers:
{ Accept: 'application/json, text/plain, */*',
'Content-Type': 'multipart/form-data',
Origin: 'http://localhost',
Authorization: ‘Bearer token',
'User-Agent': 'axios/0.15.3' },
method: 'post',
responseType: 'json',
url: ‘file.url/file',
data:
FormData {
_overheadLength: 538,
_valueLength: 98,
_valuesToMeasure: [],
writable: false,
readable: true,
dataSize: 0,
maxDataSize: 2097152,
pauseStreams: true,
_released: true,
_streams: [],
_currentStream: null,
_boundary: '--------------------------207564514352341640400188',
_events: {},
_eventsCount: 0 } },
response: undefined }
(node:89) [DEP0013] DeprecationWarning: Calling an asynchronous function without callback is deprecated.
答案 0 :(得分:0)
You are likely using an old version; this bug has been fixed in https://github.com/follow-redirects/follow-redirects/issues/50/.