1:python之上传文件

1.1.url代码

 """untitled1222 URL Configuration

 The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('upload/',views.upload),
]

1.2.views代码

 from django.shortcuts import render
from django.shortcuts import HttpResponse # Create your views here.
def upload(request):
if request.method=='GET':
return render(request,'upload.html')
else:
user=request.POST.get('user')
print(user)
#img是一个对象,包含文件名,文件大小、内容....
img=request.FILES.get('img')
print(img)
print(img.name)
print(img.size) #上传到本地服务器
f=open(img.name,'wb')
for line in img.chunks():
f.write(line)
f.close() return HttpResponse('OK')

1.3.templates中upload.html

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/upload/" method="post" enctype="multipart/form-data">
<input type="text" name="user">
<input type="file" name="img">
<input type="submit" value="提交">
</form>
</body>
</html>

1.4.效果显示

2.上传文件按钮优化

2.1按钮优化只需在原有upload.html文件中进行相关样式设置即可,重点设置透明度:opacity:0

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/upload/" method="post" enctype="multipart/form-data">
<input type="text" name="user">
<div style="position: relative;">
<a>上传</a>
<input type="file" name="img" style="opacity: 0.2;position:absolute; top:0;left: 0;">
</div>
<input type="submit" value="提交">
</form>
<script src="/static/jquery-3.3.1.min.js"></script>
</body>
</html>

2.2.优化后的效果显示:

3.python之Form组件文件上传(与上述自定义上传文件的区别在:form上传文件多了验证功能)

 from django.shortcuts import render
from django.shortcuts import HttpResponse # Create your views here.
from django import forms
from django.forms import fields
#from组件形式的文件上传
class UploadImg(forms.Form):
user=fields.CharField()
img=fields.FileField() def upload(request):
if request.method=='GET':
return render(request,'upload.html')
else:
OBJ=UploadImg(request.POST,request.FILES)
if OBJ.is_valid():
user=OBJ.cleaned_data['user']
img=OBJ.cleaned_data['img']
f = open(img.name, 'wb')
for line in img.chunks():
f.write(line)
f.close()
#自定义形式文件上传
# def upload(request):
# if request.method == 'GET':
# return render(request, 'upload.html')
# else:
# user=request.POST.get('user')
# print(user)
# #img是一个对象,包含文件名,文件大小、内容....
# img=request.FILES.get('img')
# print(img)
# print(img.name)
# print(size)
# f = open(img.name, 'wb')
# for line in img.chunks():
# f.write(line)
# f.close()
# return HttpResponse('OK')
return HttpResponse('OK')

python 文件上传本地服务器的更多相关文章

  1. ubuntu中将本地文件上传到服务器

    (1)在本地的终端下,而不是在服务器上.在本地的终端上才能将本地的文件拷入服务器. (2) scp -r localfile.txt username@192.168.0.1:/home/userna ...

  2. 基于paramiko将文件上传到服务器上

    通过安装使用paramiko模块,将本地文件上传到服务器上 import paramiko import datetime import os hostname = '服务器ip' username ...

  3. Java实现文件上传到服务器(FTP方式)

    Java实现文件上传到服务器(FTP方式) 1,jar包:commons-net-3.3.jar 2,实现代码: //FTP传输到数据库服务器 private boolean uploadServer ...

  4. Linux 文件上传Linux服务器

    进入命令行 在图形化桌面出现之前,与Unix系统进行交互的唯一方式就是借助由shell所提供的文本命令行界面(command line interface,CLI).CLI只能接受文本输入,也只能显示 ...

  5. 一、手把手教你docker搭建fastDFS文件上传下载服务器

    在搭建fastDFS文件上传下载服务器之前,你需要准备的有一个可连接的linux服务器,并且该linux服务器上已经安装了docker,若还有没安装docker的,先百度自行安装docker. 1.执 ...

  6. Linux 将本地文件上传Linux服务器, 即ssh 命令上传本地文件

    利用ssh传输文件   在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件 scp username@servername:/path/filename /var/www ...

  7. Linux 将本地文件上传Linux服务器, 即ssh 命令上传本地文件

    http://blog.csdn.net/rodulf/article/details/71169996 利用ssh传输文件 在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下 ...

  8. c#将本地文件上传至服务器(内网)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. python+selenium上传本地文件

    迅雷号自媒体视频文件自动上传,贴标签发布 难点 本地文件上传,通过send_keys(‘文件路径’)的方式实现上传的目的 文件名通过正则匹配的方式进行处理,主要匹配出中文标题名称 处理过程中文件名称中 ...

随机推荐

  1. 前端把html表格生成为excel表格

    最近公司改后台管理系统.要求导出台账项目等等为excel表格,找半天还真有,他是通过query.table2excel.js 实现,原谅我原生不会弄这个当然大家有可以给我留言. <!DOCTYP ...

  2. Chrome控制台毫无反应,打印不出信息了?

    最近在使用console.log()方法的时候遇到一个奇怪的问题,打开chrome控制台想调试代码,结果控制台半天无反应,让我纳闷了半天.详情如图所示: 然后我又打开了新的标签页,不行!接着干脆关闭浏 ...

  3. Centos7 下安装Apache2 + MySQL + PHP7

    Apache 1.安装Apache yum install httpd 2.设置服务器开机自动启动Apache systemctl enable httpd.service 若要验证是否自动启动可在重 ...

  4. GOOGLE高级搜索技巧

    前记:  我是完整的看完了.内容有点乱啊,自己没有时间整理,先放在自己的印象笔记里了....   二,GOOGLE特色 GOOGLE支持多达132种语言,包括简体中文和繁体中文: GOOGLE网站只提 ...

  5. 学习笔记:IIS搭建PHP网站出现404错误的解决办法

    关于404错误提示相信大家都遇到过吧,记得我遇到这个问题的时候,弄得我焦头烂额的,今天给大家分享下,使用IIS大家PHP网站时出现404错误提示的处理方法,希望对各位朋友有所帮助.IIS搭建PHP出现 ...

  6. leetcode summary-section II

    151 Reverse Words in a String class Solution { public: void reverseWords(string &s) { string res ...

  7. html5\CSS3有哪些新特性、移除了哪些元素?如何处理HTML5新标签的浏览器兼容问题?如何区分HTML和HTML5?

    (1)HTML5现在已经不是SGML的子集,主要是关于图像,位置,存储,地理定位等功能的增加. 绘画canvas元素: 用于媒介回放的video和audio元素: 本地离线存储localStorage ...

  8. select 1 from table 语句中的1代表什么意思

    在这里我主要讨论的有以下几个select 语句: doo_archive表是一个数据表,表的行数为4行,如下: 分别用三条select语句select  1 from doo_archive.sele ...

  9. HAProxy负载均衡保持客户端和服务器Session亲缘性的3种方式

    1 用户IP 识别  haroxy 将用户IP经过hash计算后 指定到固定的真实服务器上(类似于nginx 的IP hash 指令) 配置指令: balance source 配置实例: backe ...

  10. SpringBoot 中解决跨域请求

    CORS 理解 同源策略是web浏览器实现的一个重要的安全概念,它防止JavaScript代码对不同的来源(例如,不同的域)发出请求,而不是它所服务的来源.虽然同源策略有效地防止来自不同来源的资源,但 ...