效果

model

class WEIGUI_IMG(Base):
__tablename__ = 'DW_ZHS_D_WEIGUI_IMG' # 表名 id = Column('img_id', String(), primary_key=True)
img_context = Column(LargeBinary)
file_name = Column('file_name', String(), nullable=True)

后台路由

@api.route('/weigui/img', methods=['POST'])
def weigui_img():
img = request.files['file']
# img.save(img.filename)#直接保存为文件
weigui_img = WEIGUI_IMG()
weigui_img.id = str(uuid.uuid1()).replace('-','')
weigui_img.img_context = img.read()
weigui_img.file_name = img.filename
img.close()
db_session.add(weigui_img)
db_session.commit()
return jsonify({'msg': 'success'})

前端代码页面

                <el-form-item label="上传文件">
<el-upload action="http://10.0.0.49:9090/api/v1600/weigui/img" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
<i class="el-icon-plus"></i>
</el-upload>
</el-form-item> <el-dialog :visible.sync="dialogVisible" top="5vh">
<img width="90%" :src="dialogImageUrl" alt="">
</el-dialog>

前端代码js

                dialogImageUrl: '',
dialogVisible: false, handleRemove(file, fileList) {
console.log(file, fileList)
},
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url
this.dialogVisible = true
}

后续,编辑时取出所存图片

效果

发送请求

后台代码

需指定返回图片类型

@api.route('/weigui/img/<id>')
def weigui_img_by_id(id):
textsql = "select * from dw_zhs_d_weigui_img t where t.img_id = :id"
row = db_session.execute(textsql, {'id': id}).fetchone()
return Response(row.img_context, mimetype="image/jpeg")

返回存进去的excel文件

# 获取excel测试
@api.route('/stream/excel')
def weigui_excel():
obj = db_session.query(WEIGUI_IMG).filter(WEIGUI_IMG.file_name.like('%%%s%%' %'xls')).one()
response = Response(obj.img_context, mimetype='text/csv')
response.headers["Content-Disposition"] = "attachment; filename={}".format('1.xls')
return response

前端js代码

            //显示编辑界面
handleEdit: function (index, row) {
this.editFormVisible = true
this.editForm = Object.assign({}, row)
this.filelist = []
if(row.imgs){
let temp = row.imgs.split(',')
temp.pop()
temp.forEach(obj=>{
this.filelist.push({
name: '1.png',
url: this.imgUrl+'/'+obj
})
})
}
},

前端html代码

        <!--新增界面-->
<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
<el-form :model="addForm" label-width="80px" ref="addForm">
<el-form-item label="企业名称">
<el-select v-model="addForm.company_id" filterable remote reserve-keyword placeholder="请输入企业名" :remote-method="remoteMethod" :loading="selectLoading" style="width: 250px">
<el-option v-for="item in options" :key="item.code" :label="item.name" :value="item.code"></el-option>
</el-select>
</el-form-item>
<el-form-item label="违规时间">
<el-date-picker v-model="addForm.weigui_time" style="width: 250px"></el-date-picker>
</el-form-item>
<el-form-item label="违规详情">
<el-input v-model="addForm.detail" auto-complete="off" type="textarea" autosize></el-input>
</el-form-item>
<el-form-item label="备注">
<el-input v-model="addForm.remark" auto-complete="off" type="textarea" autosize></el-input>
</el-form-item>
<el-form-item label="文件">
<el-upload :action="imgUrl" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="handleRemove" ref="uploader_add" :on-success="fileUploadAddSuccess">
<i class="el-icon-plus"></i>
</el-upload>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click.native="addFormVisible = false">取消</el-button>
<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
</div>
</el-dialog>

python保存文件到数据库的更多相关文章

  1. PHP 合理配置实现文件上传及保存文件到数据库

    合理配置 php.ini 如何配置php.ini实现PHP文件上传功能.其中涉及到php.ini配置文件中的upload_tmp_dir.upload_max_filesize.post_max_si ...

  2. PostgreSQL保存文件到数据库

    1.CREATE TABLE public.t_file ( id INTEGER PRIMARY KEY NOT NULL DEFAULT nextval('t_file_id_seq'::regc ...

  3. C# 保存文件到数据库

    html <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUploa ...

  4. python 保存文件时候, 去除名字中的非法字符

    import re def validateTitle(title): rstr = r"[\/\\\:\*\?\"\<\>\|]" # '/ \ : * ? ...

  5. JAVA上传文件到数据库

    前端代码 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> &l ...

  6. 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)

    1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...

  7. [转] 三种Python下载url并保存文件的代码

    原文 三种Python下载url并保存文件的代码 利用程序自己编写下载文件挺有意思的. Python中最流行的方法就是通过Http利用urllib或者urllib2模块. 当然你也可以利用ftplib ...

  8. python pandas合并多个excel(xls和xlsx)文件(弹窗选择文件夹和保存文件)

    # python pandas合并多个excel(xls和xlsx)文件(弹窗选择文件夹和保存文件) import tkinter as tk from tkinter import filedial ...

  9. 【网络爬虫入门05】分布式文件存储数据库MongoDB的基本操作与爬虫应用

    [网络爬虫入门05]分布式文件存储数据库MongoDB的基本操作与爬虫应用 广东职业技术学院  欧浩源 1.引言 网络爬虫往往需要将大量的数据存储到数据库中,常用的有MySQL.MongoDB和Red ...

随机推荐

  1. 「2017 Multi-University Training Contest 2」2017多校训练2

    1001 Is Derek lying 题目链接 HDU6045 Is Derek lying? 给出两个人选择题的回答,问得分分别为x和y是否可能.(\(1\le N \le 80000,0\le ...

  2. Android 播放Gif 动画

    在Android 中是不支持直接使用Gif 图片关联播放帧动画,如下动画在Android 中是无法播放的: Android 提供了另外一种解决的办法,就是使用AnimationDrawable 这一函 ...

  3. bzoj1226/luogu2157 学校食堂 (状压dp)

    我们先约定:(左) 窗口_人人人人人 (右) 可以发现,我们只需要知道最靠左的还没打饭的人 以及它身后7个人的状态 以及上一个打饭的人是谁 因为他左面的就都打过了 右面7个人以后肯定还没打 可以设f[ ...

  4. 【php】php实现数组分块

    有时候需要将一个大数组按一定大小分块,那么可以实现这个功能,代码如下: /** * @param array $arr * @param int $size <p> * @param bo ...

  5. 闲聊javascript继承和原型

    javascript继承已经是被说烂的话题了,我就随便聊一点~ 一.javascript的复制继承 javascript的继承有复制继承和原型继承,基于复制继承用的不太多,而且无法通过instance ...

  6. Kafka学习之路

    一直在思考写一些什么东西作为2017年开篇博客.突然看到一篇<Kafka学习之路>的博文,觉得十分应景,于是决定搬来这“他山之石”.虽然对于Kafka博客我一向坚持原创,不过这篇来自Con ...

  7. git init github

    Command line instructions 执行这些命令是在windows 右菜单里面的git bash运行. Git global setup git config --global use ...

  8. MyQR库自动为网址生成二维码

    首先安装MyQR库: pip install MyQR #导包 from MyQR import myqr #生成二维码 words=你要为那个网址生成二维码 save_name=保存后的图片名 pi ...

  9. TP5.0+小程序商城构建(1)

    1.导语 1.整体的思路与编程思想(大局观.AOP面向切面编程,10-20%) 2.具体的编程知识与技巧(TP5.小程序.数据库等80%) 2.课程内容与产品技术点 1.ThinkPHP5框架 1.编 ...

  10. Python之迭代器,生成器

    迭代器 1.什么是可迭代对象 字符串.列表.元组.字典.集合都可以被for循环,说明他们都是可迭代的. from collections import Iterable l = [1,2,3,4] t ...