寫了一個投票頁面,上傳圖片的時候出了一點小插曲,隨記,以備后查。
let fd = new FormData(); //使用FormData類上傳數據
fd.append("file", this.file_img.file); /*append(key,value)向fd對象中添加一條信息
*set(key,value)更新fd對象中鍵為key的的信息
*更多FormData的屬性可以查詢相關文檔
*/
fd.append("username", this.username);
fd.append("phone", this.phone);
fd.append("id", new Date().getTime());
fd.append("type", this.$config.type);
this.$axios //使用axios發HTTP請求
.post(url, fd, {
headers: {
"Content-Type": "multipart/form-data",
charset: "UTF-8" //設置字符集為utf-8
},
transformRequest: [ //這個屬性的配置是為了解決iphon手機在文件上傳時,服務器無法獲取到數據的情況,安卓手機沒有這個問題
function() {
return fd; //這里返回的就是剛才FormData的實例對象fd
}
]
})