from django.shortcuts import render,redirect,HttpResponse
# Create your views here.
import os
import json
from Upload import models
def Upload(request):
if request.method=="GET":
# 获取所有图片
imgs=models.image.objects.all()
return render(request,"upload.html",{"imgs":imgs})
elif request.method=="POST":
# 获取普通input标签值,即文件名
filname=request.POST.get('fileName')
# 获取file类型的input标签值,即文件内容
file=request.FILES.get('fileContent')
# 获取文件后缀名
postfix=file.name.split('.')[1]
# 设置本地文件路径
file_path=os.path.join('static',filname+'.'+postfix)
# 将上传的文件写入本地目录
f=open(file_path,"wb")
for chunk in file.chunks():
f.write(chunk)
f.close()
# 写入成功后将路径保存至数据库
models.image.objects.create(file_Path=file_path)
return redirect("upload.html")
def Upload_js(request):
if request.method == "GET":
# 获取所有图片
imgs = models.image.objects.all()
return render(request, "upload_js.html", {"imgs": imgs})
elif request.method == "POST":
# 获取普通input标签值,即文件名
filname = request.POST.get('fileName')
# 获取file类型的input标签值,即文件内容
file = request.FILES.get('fileContent')
# 获取文件后缀名
postfix = file.name.split('.')[1]
# 设置本地文件路径
file_path = os.path.join('static', filname + '.' + postfix)
# 将上传的文件写入本地目录
f = open(file_path, "wb")
for chunk in file.chunks():
f.write(chunk)
f.close()
# 写入成功后将路径保存至数据库
models.image.objects.create(file_Path=file_path)
#将状态及文件路径通过json形式返回至html页面
ret={'status':True,'path':file_path}
return HttpResponse(json.dumps(ret))
def Upload_jQuery(request):
if request.method == "GET":
# 获取所有图片
imgs = models.image.objects.all()
return render(request, "upload_jQuery.html", {"imgs": imgs})
elif request.method == "POST":
# 获取普通input标签值,即文件名
filname = request.POST.get('fileName')
# 获取file类型的input标签值,即文件内容
file = request.FILES.get('fileContent')
# 获取文件后缀名
postfix = file.name.split('.')[1]
# 设置本地文件路径
file_path = os.path.join('static', filname + '.' + postfix)
# 将上传的文件写入本地目录
f = open(file_path, "wb")
for chunk in file.chunks():
f.write(chunk)
f.close()
# 写入成功后将路径保存至数据库
models.image.objects.create(file_Path=file_path)
# 将状态及文件路径通过json形式返回至html页面
ret = {'status': True, 'path': file_path}
return HttpResponse(json.dumps(ret))
def Upload_iframe(request):
if request.method == "GET":
# 获取所有图片
imgs = models.image.objects.all()
return render(request, "upload_iframe.html", {"imgs": imgs})
elif request.method == "POST":
# 获取普通input标签值,即文件名
filname = request.POST.get('fileName')
# 获取file类型的input标签值,即文件内容
file = request.FILES.get('fileContent')
# 获取文件后缀名
postfix = file.name.split('.')[1]
# 设置本地文件路径
file_path = os.path.join('static', filname + '.' + postfix)
# 将上传的文件写入本地目录
f = open(file_path, "wb")
for chunk in file.chunks():
f.write(chunk)
f.close()
# 写入成功后将路径保存至数据库
models.image.objects.create(file_Path=file_path)
# 将状态及文件路径通过json形式返回至html页面
ret = {'status': True, 'path': file_path}
return HttpResponse(json.dumps(ret))
没有合适的资源?快使用搜索试试~ 我知道了~
通过Django的form及Ajax等多种方式上传文件
共15个文件
py:11个
html:4个
需积分: 50 16 下载量 200 浏览量
2018-10-13
21:58:57
上传
评论
收藏 9KB ZIP 举报
温馨提示
通过Django的Form方式上传文件,简单,但无法局部刷新实现ajax效果; 通过js或jQuery方式,需要使用FormData对象,仍有些浏览器不兼容; 通过Form+iframe方式上传文件,兼容性最好。 里面含有全部源代码。
资源推荐
资源详情
资源评论
收起资源包目录
UploadManager.zip (15个子文件)
UploadManager
templates
upload.html 568B
upload_js.html 2KB
upload_iframe.html 1KB
upload_jQuery.html 1KB
UploadManager
wsgi.py 419B
__init__.py 0B
settings.py 3KB
urls.py 990B
static
manage.py 560B
Upload
models.py 162B
apps.py 92B
__init__.py 0B
views.py 4KB
admin.py 66B
tests.py 63B
共 15 条
- 1
资源评论
baobao267
- 粉丝: 21
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功