方案一
在node中使用axios以post的方式發送一張圖片給某個server時:
let data = fs.createReadStream(__dirname + '/test.jpg')
axios.post(url,{media:data,type:"image"})
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
})
方案二
事實證明,這樣做是完全沒有用的,我嘗試向另一個服務器poststream,返回的總是錯誤。然而,如果我使用request,下面這樣的代碼是完全沒有問題的:
let data = fs.createReadStream(__dirname + '/test.jpg')
let form = {
type:"image",
media:data
}
request.post({url:url,formData:form},(err,res,body)=>{
if(err) console.log(err)
console.log(body)
})
// 注冊事件方法
register: function() {
let registerUrl = this.GetServerUrl() + "/user/signup";
let params = {
username: this.username,
password: this.password,
email: this.email,
captcha: this.captcha
};
this.axios
.post(registerUrl, params)
.then(response => {
if(response.status == 0) {
}
});
}