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. Laravel基本使用

    laravel一.简介二.运行环境要求 1.php 版本>=5.5.9 2.Mcrypt PHP扩展 php的加密扩展,提供多种加密算法 3.openssl扩展 对传输的数据进行加密 4.mbs ...

  2. 微服务架构之spring cloud gateway

    Spring Cloud Gateway是spring cloud中起着非常重要的作用,是终端调用服务的入口,同时也是项目中每个服务对外暴露的统一口径,我们可以在网关中实现路径映射.权限验证.负载均衡 ...

  3. python判断一个数字是整数还是浮点数&判断整除

    判断整数还是浮点数   >>> a=123 >>> b=123.123 >>> isinstance(a,int) True >>&g ...

  4. 图片裁剪(基于RxPaparazzo)

    图片裁剪(基于RxPaparazzo) 前言:基于RxPaparazzo的图片裁剪,图片旋转.比例放大|缩小. 效果: 开发环境:AndroidStudio2.2.1+gradle-2.14.1 涉及 ...

  5. BIEE总结

    一,数据仓库,BI涉及到的相关概念  1.DW:    即数据仓库(Data Warehouse),是一个面向主题的(Subject Oriented).集成的(Integrated).相对稳定的(N ...

  6. 关于VSTS自动Build报错问题之Microsoft.Net.Compilers

    报错内容如下: --06T11::.6035712Z ##[error]Dotnet command failed with non-zero exit code on the following p ...

  7. 乘风破浪:LeetCode真题_008_String to Integer (atoi)

    乘风破浪:LeetCode真题_008_String to Integer (atoi) 一.前言 将整型转换成字符串,或者将字符串转换成整型,是经常出现的,也是必要的,因此我们需要熟练的掌握,当然也 ...

  8. angularJs的$scope.$apply

    <!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" ...

  9. 使用jenkins SonarQube gitlab 构建自动化发布系统

    目前持续集成的生态越来越完善,工具也有很多,开源的或商业的.如: 最最流行的,也是使用最多的 Jenkins 有着持续集成DNA的ThoughtWorks GO.理念:"Deployment ...

  10. P1314 聪明的质监员

    题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1 到 n 逐一编号,每个矿石都有自己的重量 \(w_i\)​ 以及价值 \(v_i\) .检验矿产的流程是 ...