python 文件上传本地服务器
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 文件上传本地服务器的更多相关文章
- ubuntu中将本地文件上传到服务器
(1)在本地的终端下,而不是在服务器上.在本地的终端上才能将本地的文件拷入服务器. (2) scp -r localfile.txt username@192.168.0.1:/home/userna ...
- 基于paramiko将文件上传到服务器上
通过安装使用paramiko模块,将本地文件上传到服务器上 import paramiko import datetime import os hostname = '服务器ip' username ...
- Java实现文件上传到服务器(FTP方式)
Java实现文件上传到服务器(FTP方式) 1,jar包:commons-net-3.3.jar 2,实现代码: //FTP传输到数据库服务器 private boolean uploadServer ...
- Linux 文件上传Linux服务器
进入命令行 在图形化桌面出现之前,与Unix系统进行交互的唯一方式就是借助由shell所提供的文本命令行界面(command line interface,CLI).CLI只能接受文本输入,也只能显示 ...
- 一、手把手教你docker搭建fastDFS文件上传下载服务器
在搭建fastDFS文件上传下载服务器之前,你需要准备的有一个可连接的linux服务器,并且该linux服务器上已经安装了docker,若还有没安装docker的,先百度自行安装docker. 1.执 ...
- Linux 将本地文件上传Linux服务器, 即ssh 命令上传本地文件
利用ssh传输文件 在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件 scp username@servername:/path/filename /var/www ...
- Linux 将本地文件上传Linux服务器, 即ssh 命令上传本地文件
http://blog.csdn.net/rodulf/article/details/71169996 利用ssh传输文件 在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下 ...
- c#将本地文件上传至服务器(内网)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- python+selenium上传本地文件
迅雷号自媒体视频文件自动上传,贴标签发布 难点 本地文件上传,通过send_keys(‘文件路径’)的方式实现上传的目的 文件名通过正则匹配的方式进行处理,主要匹配出中文标题名称 处理过程中文件名称中 ...
随机推荐
- Centos7 下安装Apache2 + MySQL + PHP7
Apache 1.安装Apache yum install httpd 2.设置服务器开机自动启动Apache systemctl enable httpd.service 若要验证是否自动启动可在重 ...
- restful知识点之三restframework认证-->权限-->频率
认证.权限.频率是层层递进的关系 权限业务时认证+权限 频率业务时:认证+权限+频率 局部认证方式 from django.conf.urls import url,include from djan ...
- escape,encodeURI,encodeURIComponent, URLEncode, RawURLEncode, HTMLEntity, AddSlash, JSON Encode
online tooling: http://www.the-art-of-web.com/javascript/escape/ input : {user:{id:59,innerhtml:&quo ...
- Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)
前言:本文是我撰写的关于搭建“Nginx + PHP(FastCGI)”Web服务器的第6篇文章.本系列文章作为国内最早详细介绍 Nginx + PHP 安装.配置.使用的资料之一,为推动 Nginx ...
- Python实例---简单购物车Demo
简单购物车Demo # version: python3.2.5 # author: 'FTL1012' # time: 2017/12/7 09:16 product_list = ( ['Java ...
- 沉淀再出发:PHP的中级内容
沉淀再出发:PHP的中级内容 一.前言 前面我们介绍了PHP的简单的语法知识以及相关的用法,接下来我们将PHP+mysql以及PHP+ajax结合起来进行研究. 二.PHP+mysql ...
- npm 使用国内镜像的方法
npm全称Node Package Manager,是node.js的模块依赖管理工具.由于npm的源在国外,所以国内用户使用起来各种不方便.我们通过设置使用淘宝的镜像来加快我们的速度. 临时使用 n ...
- bzoj5153 [Wc2018]州区划分
题目链接 正解:子集和变换. 考场上只会暴力和$p=0$的情况,还只会$O(2^{n}*n^{3})$的. 然而这题题面出锅,导致考场上一直在卡裸暴力,后面的部分分没写了..听$laofu$说$O(2 ...
- - (void)addConstraints:(NSArray<__kindof NSLayoutConstraint *> *)constraints
Adds multiple constraints on the layout of the receiving view or its subviews. All constraints mus ...
- Mysql分区表及自动创建分区Partition
Range分区表建表语句如下,其中分区键必须和id构成主键和唯一键 CREATE TABLE `test1` ( `id` char(32) COLLATE utf8mb4_unicode_ci NO ...