使用FormData对象来创建请求体,并将Content-Type设置为undefined。
示例代码如下:
const formData = new FormData();
formData.append('file', file);
axios({
method: 'put',
url: 'your-api-endpoint',
data: formData,
headers: {
'Content-Type': undefined
}
}).then(response => {
// handle response
}).catch(error => {
// handle error
});
其中,file为通过上传的文件对象。使用FormData对象可以轻松地将文件添加到请求体中,并将Content-Type设置为undefined可以让浏览器自动设置合适的请求头。