我一直在用这个将图像文件上传到 API(Graphcool),一切正常:
fileUpload(file) {
let data = new FormData();
data.append('data', file);
axios
.post(`https://api.graph.cool/file/v1/MY-PROJECTID`, data, {
headers: {
'Content-Type': 'multipart/form-data',
},
})
.then(res => {
console.log(res)
});
}
在上面的代码中,文件是从<input type="file" />
但是现在我使用 React Avatar Editor 来允许用户裁剪图像并确保它们是正方形: https ://github.com/mosch/react-avatar-editor
当您从 React Avatar Editor 访问图像时,它以数据 url 的形式出现(通过 Canvas.toDataURL())。
如何使用 Axios 上传数据 url?我是否需要先将图像转换为浏览器内存中的实际“文件”?