解决方案如下
import axios from 'axios'
import * as fs from 'fs'
import { HttpDelegate } from './HttpKit'
import * as FormData from 'form-data'
import * as https from 'https'
import concat = require('concat-stream')
upload(url: string, filePath: string, params?: string): Promise {
return new Promise((resolve, reject) => {
let formData = new FormData()
formData.append('media', fs.createReadStream(filePath))
if (params) {
formData.append('description', params)
}
formData.pipe(
concat({ encoding: 'buffer' }, async data => {
axios
.post(url, data, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
.then(response => {
if (response.status === 200) {
resolve(JSON.stringify(response.data))
} else {
reject(`error code ${response.status}`)
}
})
.catch(error => {
reject(error)
})
})
)
})
}