门户网站建设和管理情况,wordpress 单栏 宽屏,html个人网站,wordpress插件支付宝积分可以通过以下步骤从服务端获取图片#xff1a; 引入axios库#xff1a;在前端代码中使用axios库来发送HTTP请求。可以通过以下方式引入axios#xff1a; import axios from axios;发送请求#xff1a;使用axios发送HTTP请求#xff0c;获取图片文件的二进制数据。发送请求…可以通过以下步骤从服务端获取图片 引入axios库在前端代码中使用axios库来发送HTTP请求。可以通过以下方式引入axios import axios from axios;发送请求使用axios发送HTTP请求获取图片文件的二进制数据。发送请求的代码示例 axios({method: GET,url: http://your-domain.com/path/to/image.jpg,responseType: arraybuffer
}).then(response {// 将响应的二进制数据转换为Blob对象const blob new Blob([response.data], { type: response.headers[content-type] });// 根据Blob对象生成图片URLconst imgUrl URL.createObjectURL(blob);// 将图片URL赋值给图片元素的src属性const imgElement document.querySelector(#my-img);imgElement.src imgUrl;
});在上面的代码中responseType设置为arraybuffer表示响应的数据以二进制数组的形式返回。在响应成功的回调函数中将响应的二进制数据转换为Blob对象并根据Blob对象生成图片URL最后将图片URL赋值给图片元素的src属性即可显示图片。 axios({method: GET,url: http://your-domain.com/path/to/getImage,responseType: blob}).then(response {// 将响应的二进制数据转换为Blob对象const blob new Blob([response.data], { type: response.headers[content-type] });// 根据Blob对象生成图片URL 将图片URL赋值给图片元素的src属性const reader new FileReader();reader.readAsDataURL(blob);reader.onload function (e) {imgUrl e.target.result;// e.target.result 即为base64结果};reader.onerror (error) {console.log(图形加载错误);};});在上面的代码中responseType设置为blob表示响应的数据以blob对象的形式返回。在响应成功的回调函数中将响应的二进制数据转换为Blob对象并根据Blob对象生成图片URL最后将图片URL赋值给图片元素的src属性即可显示图片。
以上虽然设置了blob但是根本上还是图片以二进制的形式传输的因为所有非json格式均可以以blob接收在转换成对应格式的图片或文件