Last active
June 26, 2020 02:29
-
-
Save isayme/942afbecf64b0f196001d6ef27879bd1 to your computer and use it in GitHub Desktop.
服务端使用 request & axios 上传文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* request 背后也是用的 form-data. 符合浏览器的 window.FromData 规范生产 body 及 header 信息. | |
* smfile 需要是 stream, 从而判定为文件. | |
* 另: formstream 应该也可以, 但没有测试过. | |
*/ | |
const fs = require('fs') | |
const axios = require('axios') | |
const request = require('request-promise') | |
const FormData = require('form-data') | |
let form = new FormData() | |
form.append('smfile', fs.createReadStream(__dirname + '/teambition-icon100.png')) | |
axios.request({ | |
method: 'POST', | |
url: 'https://sm.ms/api/upload', | |
headers: form.getHeaders(), | |
data: form | |
}).then(console.log, console.log) | |
request({ | |
method: 'POST', | |
url: 'https://sm.ms/api/upload', | |
headers: { | |
'user-agent': 'Mozilla/5.0' | |
}, | |
formData: { | |
smfile: fs.createReadStream(__dirname + '/teambition-icon100.png') | |
} | |
}).then(console.log, console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment