Vue+Flask实现图片传输功能
作者:小周bb 时间:2024-05-21 10:15:34
本文实例为大家分享了Vue+Flask实现图片传输功能的具体代码,供大家参考,具体内容如下
完整流程:
1.图片转为formdata 传输到后端
2.后端接收后,确定文件后缀名无误,修改文件名并保存到固定的路径中
3.前端请求图片
4.后端根据图片名字返回图片数据流
5.前端将数据流处理,转为图片
1.Vue上传
<template>
? ? <div>
? ? ? ? <input type="file" class="file" id ='file'>?
? ? ? ? <input type="submit" @click="uploadClick">
? ? </div>
</template>
<script>
export default {
? ?
? ? data () {
? ? ? ? return {}
? ? },
? ? methods:{
? ? ? ? uploadClick(){
? ? ? ? ? ? console.log(document.getElementById('file').files[0]);
? ? ? ? ? ? var formData = new window.FormData()?
? ? ? ? ? ? formData.append('file',document.getElementById('file').files[0])
? ? ? ? ? ? upload(formData)
? ? ? ? ? ??
? ? ? ? } ??
? ? }
}
</script>
//这是封装的请求
export const upload = (file) => {
? return request({
? ? url: '/api/uavprp/upload',
? ? data: file,
? ? method: 'post',
? ? headers: {?
? ? ? 'Content-Type': 'multipart/form-data'
? ? ?},
? })
}
2.Flask接收
#请求中获取到上传的图片
a = request.files.get('file')
#限制文件后缀名必须为图片的类型
allowed_filename = set(['png', 'jpg', 'JPG', 'PNG'])
isallowed = '.' in filename and filename.rsplit('.', 1)[1] in allowed_filename
path = basedir + "/image/" ?#这里的image是自己创建的保存图片文件夹路径
img_name = change_file_name(a.filename,ImgID)#这是我自己定义的一个方法 为了修改图片的名字为随机生成的唯一ID,同时不修改后缀名
file_path = path + img_name#完整的保存路径加图片名
a.save(file_path)#保存
3.Vue请求图片并转换返回的数据流为图片展示
<template>
?? ?<div>
? ? ? <img :src="ferUrl"/>
? ? </div>
</template>
<script>
export default {
? ?
? ? data () {
? ? ? ? return {
?? ??? ??? ?imgUrl: "",
?? ??? ?}
? ? },
? ? created(){
?? ??? ?getImg("ImgNXTIP298.jpg").then((res) => {
? ? ? ? this.imgUrl = window.URL.createObjectURL(res.data);//这里请求接收的一定是Blod类型数据
? ? ? ? console.log(this.imgUrl);
? ? ? });
?? ?}
}
</script>
//封装的图片请求
export const getImg = (imgID) => {
? return request({
? ? url: '/api/ferchoosen/getFerImg',
? ? method: 'get',
? ? responseType:"blob",
? ? params: {
? ? ? imgID
? ? }
? })
}
4.Flask接收前端请求返回图片数据流
imgID = request.args["imgID"]
? ? if imgID == None:
? ? ? ? return output(msg="该图片不存在")
? ? path = basedir + "/image/"
? ? image_data = open(os.path.join(path, '%s' % imgID), "rb").read()
? ? response = make_response(image_data)
? ? response.headers['Content-Type'] = 'image/png'#返回的内容类型必须修改
? ? return response
来源:https://blog.csdn.net/aaaaaaaaaww12/article/details/121608648
标签:Vue,Flask,图片传输
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
自己写的一个PJBlog中可以双击输入验证码的修改
2009-05-17 10:51:00
python实现远程通过网络邮件控制计算机重启或关机
2022-04-16 01:05:27
![](https://img.aspxhome.com/file/2023/9/110599_0s.jpg)
Mongoose经常返回e11000 error的原因分析
2024-05-03 15:36:05
python实现合并两个有序列表的示例代码
2021-06-02 20:07:29
![](https://img.aspxhome.com/file/2023/9/130749_0s.png)
django 数据库 get_or_create函数返回值是tuple的问题
2024-01-27 09:47:22
微信小程序的部署方法步骤
2024-04-10 10:39:53
![](https://img.aspxhome.com/file/2023/3/137053_0s.png)
keras实现theano和tensorflow训练的模型相互转换
2023-04-18 05:49:26
![](https://img.aspxhome.com/file/2023/1/89081_0s.jpg)
PHP生成sitemap.xml地图函数
2024-06-05 09:23:16
Pandas 同元素多列去重的实例
2023-02-09 21:03:32
mysql5.7.19 安装配置方法图文教程(win10)
2024-01-16 03:52:10
![](https://img.aspxhome.com/file/2023/8/79018_0s.png)
Windows8下安装Python的BeautifulSoup
2022-05-30 15:03:06
Vue中插槽slot的使用方法与应用场景详析
2023-07-02 17:04:11
![](https://img.aspxhome.com/file/2023/6/139916_0s.png)
Golang中omitempty关键字的具体实现
2024-04-25 15:12:55
提高javascript的速度系列(序)
2008-07-15 12:38:00
PHP APC缓存配置、使用详解
2023-11-21 22:15:15
python+gdal+遥感图像拼接(mosaic)的实例
2023-02-22 23:40:34
影响SEO的页面制作细节
2008-10-18 16:06:00
盘点Python 爬虫中的常见加密算法
2023-07-19 05:05:22
![](https://img.aspxhome.com/file/2023/5/124245_0s.jpg)
SQL Server ISNULL 不生效原因及解决
2024-01-13 09:53:34
js原生map实现的方法总结
2024-04-10 10:47:34