python图片上传
Posted 时间影像
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python图片上传相关的知识,希望对你有一定的参考价值。
1.前台 <form method="post" action="./writerApply" enctype="multipart/form-data"> <br> <input type="text" name="realname" placeholder="真实姓名" class=" rowspace form-control " style="margin-bottom: 3px"> <input type="text" name="idnumber" placeholder="身份证号" class="form-control" style="margin-bottom: 3px"> <input type="number" name="telnumber" placeholder="电话号码" class="form-control" style="margin-bottom: 3px"> <input type="email" name="qq" placeholder="QQ号" class="form-control" style="margin-bottom: 3px"> <input type="text" name="biming" placeholder="笔名" class="form-control" style="margin-bottom: 3px"> <h4>上传身份证照片</h4><input type="file" name="idimage" id="uploading_id" onchange="xmTanUploadImg(this)" accept="image/*"> <img id="xmTanImg" width="300px" height="300px"/> <h4>个人手持身份证照片</h4><input type="file" name="idperson" id="uploading_id_person" onchange="xmTanUploadImg(this)" accept="image/*"> <img id="xmTanImg1" width="300px" height="300px"/><br> <textarea rows="5" cols="10" placeholder="申请说明" class="form-control" name="appexplain"></textarea> {% csrf_token %} <input type="submit" value="提交申请"> </form> 2.后台 def writerApply(request): try: loginbean = request.session[‘loginbean‘] if loginbean==None: return HttpResponse("<script>alert(‘登录过期,请重新登录‘);location.href=‘/‘;</script>") if request.method != ‘POST‘: return render(request, ‘home/writerApply.html‘) else: dict = request.POST.dict() del dict[‘csrfmiddlewaretoken‘] idimage = request.FILES.get(‘idimage‘) if idimage == None: return HttpResponse(‘必须上传身份证照片‘) exit(0) idperson = request.FILES.get(‘idperson‘) if idperson == None: return HttpResponse(‘必须上传手持身份证照片‘) exit(0) try: #改图片名字另存为 idimagePath = "%s1%s"%(time.time(),idimage.name) f = open(os.path.join("manager\\static\\imgs",idimagePath), ‘wb‘) for chunk in idimage.chunks(chunk_size=1024): f.write(chunk) dict[‘idimage‘] = idimagePath idpersonPath = "%s2%s" % (time.time(), idperson.name) f = open(os.path.join("manager\\static\\imgs",idpersonPath), ‘wb‘) for chunk in idperson.chunks(chunk_size=1024): f.write(chunk) dict[‘idperson‘] = idpersonPath writer = Writers.objects.create(createtime=time.strftime(‘%Y-%m-%d %H:%M:%S‘, time.localtime(time.time())), **dict) print(writer) except Exception as e: print(e) finally: f.close() return HttpResponse(‘上传成功‘) #入库操作 # print(dict) # realname = request.POST.get(‘realname‘) # return HttpResponse(dict[‘realname‘]) #writer = Writers.objects.create(createtime=time.strftime(‘%Y-%m-%d %H:%M:%S‘, time.localtime(time.time())),**dict) # **dict必须放到最后 except Exception as err: print(err) return HttpResponse("<script>alert(‘网页错误‘);</script>")
以上是关于python图片上传的主要内容,如果未能解决你的问题,请参考以下文章