path="cert/cert_14.png" curl -F image=@${path} 'http://9.135.88.111:8080/StructLLM' 这里前端如何用axios 去请求呢
时间: 2025-02-09 20:16:57 浏览: 38
在前端使用Axios库发送POST请求,并附带二进制文件(图片)时,需要将路径转换成`FormData`对象的形式。这里是一个例子,展示如何替换掉上述`curl`命令中的内容,使用Axios:
```javascript
const axios = require('axios');
// 定义文件路径变量
const imagePath = 'cert/cert_14.png';
// 使用 Axios 的 `uploadFile` 或者 `post` 方法,创建一个 FormData 对象并添加文件
async function uploadImage() {
const file = new File([await fetch(imagePath).then(res => res.arrayBuffer())], 'cert_14.png', {type: 'image/png'});
const formData = new FormData();
formData.append('image', file);
try {
// 发送 POST 请求到指定 URL
const response = await axios.post(
'http://9.135.88.111:8080/StructLLM',
formData,
{
headers: {
'Content-Type': 'multipart/form-data'
}
}
);
console.log(response.data); // 打印服务器返回的数据
} catch (error) {
console.error('Error uploading image:', error);
}
}
uploadImage(); // 调用函数上传图片
```
在这个例子中,我们首先读取文件的内容为ArrayBuffer,然后将其添加到`FormData`对象中。`Content-Type`头也被设置为`multipart/form-data`,这是发送二进制数据的标准方式。
阅读全文
相关推荐


















