<input id="fileUpload" type="file" class="choose-file-input" v-on:change="addProject"/>
addProject: function () {
let that = this;
var formData = new FormData();
formData.append('sketchfile', $('#fileUpload')[0].files[0]);
$.ajax({
url: 'http://api.uitocode.oa.com/project/add',
type: 'POST',
cache: false,
data: formData,
processData: false,
contentType: false
}).done(function(res) {
console.log('上傳成功');
}).fail(function(res) {});
}
要注意的是,文件上傳,走ajax的話,contentType須要定義,否則會出錯,切記!!
若是實在有問題,就本地用input直接定義上傳一下試試,
參考地址:https://www.jianshu.com/p/46e6e03a0d53
文件下載:
<form action="http://...." method="POST" enctype="application/x-www-form-urlencoded">
<input name="id" value="123123" type="hidden">
<input name="downloadSketch" value="true" type="hidden">
<button class="download-btn" type="submit">Download Cod</button>
</form>
action:下載的接口地址;
input的name:下載的參數名稱;
input的value:下載的參數,若是是在vue中,不能用value,得用v-model;
input的type:隱藏;
但願本文對你有所幫助!