Don*_*the 85 javascript ajax file-upload axios
当我使用以下内容将文件发布到烧瓶服务器时使用原始.html我可以访问烧瓶请求全局中的文件:
<form id="uploadForm" action='upload_file' role="form" method="post" enctype=multipart/form-data>
<input type="file" id="file" name="file">
<input type=submit value=Upload>
</form>
Run Code Online (Sandbox Code Playgroud)
在烧瓶中:
def post(self):
if 'file' in request.files:
....
Run Code Online (Sandbox Code Playgroud)
当我尝试对Axios执行相同操作时,求助栏请求全局为空:
<form id="uploadForm" enctype="multipart/form-data" v-on:change="uploadFile">
<input type="file" id="file" name="file">
</form>
uploadFile: function (event) {
const file = event.target.files[0]
axios.post('upload_file', file, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
}
Run Code Online (Sandbox Code Playgroud)
如果我使用上面的相同uploadFile函数但是从axios.post方法中删除了头json,我在我的flask请求对象的表单键中获得了一个字符串值的csv列表(文件是.csv).如何获取通过axios发送的文件对象?
Nik*_*aut 180
将文件添加到formData对象,并将Content-Type标头设置为multipart/form-data.
var formData = new FormData();
var imagefile = document.querySelector('#file');
formData.append("image", imagefile.files[0]);
axios.post('upload_file', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
Run Code Online (Sandbox Code Playgroud)
jaf*_*mlp 20
分享我使用 React 和 HTML 输入的经验
定义输入字段
<input type="file" onChange={onChange} accept ="image/*"/>
Run Code Online (Sandbox Code Playgroud)
定义 onChange 监听器
const onChange = (e) => {
let url = "https://<server-url>/api/upload";
let file = e.target.files[0];
uploadFile(url, file);
};
const uploadFile = (url, file) => {
let formData = new FormData();
formData.append("file", file);
axios.post(url, formData, {
headers: {
"Content-Type": "multipart/form-data",
},
}).then((response) => {
fnSuccess(response);
}).catch((error) => {
fnFail(error);
});
};
const fnSuccess = (response) => {
//Add success handling
};
const fnFail = (error) => {
//Add failed handling
};
Run Code Online (Sandbox Code Playgroud)
小智 9
这对我有用,我希望对某人有所帮助。
var frm = $('#frm');
let formData = new FormData(frm[0]);
axios.post('your-url', formData)
.then(res => {
console.log({res});
}).catch(err => {
console.error({err});
});
Run Code Online (Sandbox Code Playgroud)
小智 9
如果您不想使用FormData对象(例如,您的 API 采用特定的内容类型签名并且multipart/formdata不是其中之一),那么您可以这样做:
uploadFile: function (event) {
const file = event.target.files[0]
axios.post('upload_file', file, {
headers: {
'Content-Type': file.type
}
})
}
Run Code Online (Sandbox Code Playgroud)
小智 8
使用Vue的示例应用程序。需要在本地主机上运行的后端服务器来处理请求:
var app = new Vue({
el: "#app",
data: {
file: ''
},
methods: {
submitFile() {
let formData = new FormData();
formData.append('file', this.file);
console.log('>> formData >> ', formData);
// You should have a server side REST API
axios.post('http://localhost:8080/restapi/fileupload',
formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
}
).then(function () {
console.log('SUCCESS!!');
})
.catch(function () {
console.log('FAILURE!!');
});
},
handleFileUpload() {
this.file = this.$refs.file.files[0];
console.log('>>>> 1st element in files array >>>> ', this.file);
}
}
});
Run Code Online (Sandbox Code Playgroud)
https://codepen.io/pmarimuthu/pen/MqqaOE
小智 7
这是我的方式:
var formData = new FormData(formElement);
// formData.append("image", imgFile.files[0]);
const res = await axios.post(
"link-handle",
formData,
{
headers: {
"Content-Type": "multipart/form-data",
},
}
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
118788 次 |
| 最近记录: |