Python+django实现文件上传
作者:hebedich 时间:2022-08-31 20:52:22
1、文件上传(input标签)
(1)html代码(form表单用post方法提交)
<input class="btn btn-primary col-md-1" style="margin:0px 15px 25px 15px;" id="submitForm" type="button" value="提交" />
<form id="picture_form" action="/addForm/"enctype="multipart/form-data" method="post">
<table>
表格
</table>
</form>
(2)jq提交表单到后台
$("#submitForm").click(function(){
//alert($("#SelectBus").val());
addNameForm();//因为是动态加载的表单内容,所以会用函数给所用标签符name值
$.ajaxSetup({
async : false
});
$("#picture_form").ajaxSubmit({
resetForm:false,
dataType:'json',
success:function(data){
if(data=1){alert("提交成功");}
else{alert("提交失败");}
}
});
});
(3)python后台接受处理表单所传内容,主要file处理
#自定义存储路径
rollfileName="webStatic/uploadfile/files/"
rollfilePath=os.path.join(basePath,rollfileName)
# req.POST.get(text[1],'')如果获取到信息,则值不是123,如果是空,没有获取到信息结果是123
if req.POST.get(text[1],'123')=='123':
# 获取文件二进制流
reqfile = req.FILES[text[1]]
# 获取文件名后缀
filetype=reqfile.name.split(".")[-1]
# 生成随机字符串加后缀的文件名
filename=str(uuid.uuid1())+'.'+filetype
# 打开文件存储路径
of = open(rollfilePath+filename, 'wb+')
# 向指定路径写入文件
for chunk in reqfile.chunks():
of.write(chunk)#写入内容
of.close()#关闭连接
18 #在数据库中存储路径rollfileName+filename
(4)python后台处理用到的包
1 #生成无序字符串,替换文件名
2 import uuid
标签:Python,django,文件上传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一段ASP的HTTP_REFERER判断代码
2011-03-25 10:41:00
Python3使用xlrd、xlwt处理Excel方法数据
2023-04-03 03:25:29
![](https://img.aspxhome.com/file/2023/6/82766_0s.png)
SQL学习笔记八 索引,表连接,子查询,ROW_NUMBER
2011-09-30 11:18:24
网马解密大讲堂——网马解密中级篇(Document.write篇)
2009-09-16 16:16:00
基于Django用户认证系统详解
2023-04-13 15:35:13
ORACLE应用经验(2)
2010-07-31 13:31:00
python基础入门学习笔记(Python环境搭建)
2022-01-12 20:27:48
![](https://img.aspxhome.com/file/2023/2/71502_0s.png)
深入解析PHP 5.3.x 的strtotime() 时区设定 警告信息修复
2023-11-06 19:25:27
html元素input使用方法
2007-12-06 13:02:00
python Requsets下载开源网站的代码(带索引 数据)
2023-01-03 13:19:11
MySQL安全大讲堂:MySQL数据库安全配置
2009-10-18 11:24:00
When we`re only No.2, we try harder之淘宝节日LOGO互动设计小探讨
2010-01-20 10:31:00
![](https://img.aspxhome.com/file/UploadPic/20101/20/taobao_logoes-1-94s.jpg)
一文带你掌握Python中文词频统计
2022-11-17 21:02:00
![](https://img.aspxhome.com/file/2023/0/66880_0s.png)
使用PHP获取网络文件的实现代码
2023-09-09 08:41:53
统一的品牌设计
2008-08-07 12:53:00
![](https://img.aspxhome.com/file/UploadPic/20088/7/200887125533323s.jpg)
Access与Flash的结合应用
2008-11-20 16:44:00
css可以给img元素设置背景图
2008-09-29 15:35:00
利用pip安装python第三方库的4种方法
2023-11-16 08:16:51
![](https://img.aspxhome.com/file/2023/0/63570_0s.png)
网页特效文字之—压纹字
2023-06-26 19:30:06
![](https://img.aspxhome.com/file/UploadPic/20072/2007231160904s.jpg)
约瑟夫问题的Python和C++求解方法
2023-12-05 15:09:28