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. 学习servlet心得

    1,关于字符编码问题: // resp.setCharacterEncoding("UTF-8");//这个的作用仅仅只是输出字符,不做格式转换成HTML // resp.setC ...

  2. Linux开篇

    1.为什么学习Linux? 2.学习Linux的资料

  3. Maximum Depth of Binary Tree 二叉树的深度

    Given a binary tree,find its maximum depth. The maximum depth is the number of nodes along the longe ...

  4. 【眼见为实】自己动手实践理解数据库REPEATABLE READ && Next-Key Lock

    [REPEATABLE READ] 首先设置数据库隔离级别为可重复读(REPEATABLE READ): set global transaction isolation level REPEATAB ...

  5. mongodb shell警告

    # mongoMongoDB shell version: 3.0.2connecting to: testServer has startup warnings: 2015-05-09T12:34: ...

  6. Spring Boot框架搭建

    用idea搭建Springboot还是很方便的 环境变量是JDK1.8 SpringBoot自带了Tomcat启动也很方便 1.创建项目 2. 3.选择SpringBoot的版本以及组件 4.创建完成 ...

  7. javascript 字面量

    https://www.cnblogs.com/radius/p/6874165.html

  8. python之内置函数,匿名函数

    什么是内置函数? 就是Python给你提供的,拿来直接用的函数,比如print,input等等.其实就是我们在创建.py的时候python解释器所自动生成的内置的函数,就好比我们之前所学的作用空间 内 ...

  9. iOS自动化-- 常用iOS命令

    iOS命令: 获取设备的的UDID idevice_id --list # 显示当前所连接设备的 udid instruments -s devices # 列出所有设备,包括真机.模拟器.mac i ...

  10. Java 运算符(引用和对象)

    1. 算数运算符 就是+.-.*./.%.++.--这些,没什么好说的,稍微强调下自加,自减: 前缀自增自减法(++i,--i): 先进行自增或者自减运算,再进行表达式运算. 后缀自增自减法(i++, ...