2.8.5Django --9.1 文件上传

Django目录:https://blog.csdn.net/qq_41106844/article/details/105554082

简单的文件上传

关于文件上传,普通网站最常用的就是头像上传,有一些功能性网站会有文档或者视频上传功能。其实文件的上传本质还是一串二进制流的存储变换。我们来用基础的form表单来举个例子:

two_exa.two_exa.urls.py
-----------------------
from django.contrib import admin
from django.urls import path
from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index1/',views.index1),
    path('file_put/',views.file_put)
]

two_exa.app01.views.py
----------------------
from django.shortcuts import render,HttpResponse
import os
from two_exa import settings

def index1(request):
    return render(request,"index1.html")

def file_put(request):
    print(request.POST)
    print(request.FILES)
    file_obj = request.FILES.get("file_obj")
    print(file_obj)
    path = file_obj.name
    #settings.BASE_DIR是项目根目录的意思。
    path = os.path.join(settings.BASE_DIR,"media","img",path)
    with open(path,"wb") as f:
        for line in file_obj:
            f.write(line)
    return HttpResponse("ok")


two_exa.tmplates.index1.html
----------------------------
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>简单的文件上传</p>
<form action="/file_put/" method="post" enctype="multipart/form-data">
姓名<input type="text" name="user">
文件<input type="file" name="file_obj">
    <input type="submit" >
</form>
</body>
</html>

我们随便选择一个文件上传。

 

 
20155953-17f3214b7a9ad95b.png
页面截图1

 

上传成功

 

 
20155953-f2ea64df408932aa.png
页面截图2

 

 

我们把文件上传到了我们指定的目录。

 

 

 
20155953-1891a88dd43121fc.png
目录树部分

 

ajex文件上传

既然能使用普通的form表单,那么我们用ajex来尝试一下。

two_exa.two_exa.urls.py
-----------------------
from django.contrib import admin
from django.urls import path
from app01 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('index1/',views.index1),
    path('file_put/',views.file_put)
]

two_exa.app01.views.py
----------------------
from django.shortcuts import render,HttpResponse
import os
from two_exa import settings

def index1(request):
    return render(request,"index1.html")

def file_put(request):
    print(request.POST)
    print(request.FILES)
    file_obj = request.FILES.get("file_obj")
    print(file_obj)
    path = file_obj.name
    #settings.BASE_DIR是项目根目录的意思。
    path = os.path.join(settings.BASE_DIR,"media","img",path)
    with open(path,"wb") as f:
        for line in file_obj:
            f.write(line)
    return HttpResponse("ok")


two_exa.tmplates.index1.html
----------------------------
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript"  src="{% static 'js/jquery.js' %}"></script>
</head>
<body>
<p>简单的文件上传</p>
<form action="/file_put/" method="post" enctype="multipart/form-data">
姓名<input type="text" name="user">
文件<input type="file" name="file_obj">
    <input type="submit" >
</form>
<hr>
<p>ajex的文件上传</p>
<div>
    用户名 <input type="text" id="user">
    头像 <input type="file" id="avatar">
    <input type="button" id="ajax-submit" value="ajax-submit">
</div>
<script>
        $("#ajax-submit").click(function(){
        var formdata=new FormData();
        formdata.append("user",$("#user").val());
        formdata.append("file_obj",$("#avatar")[0].files[0]);
        $.ajax({

            url:"/file_put/",
            type:"post",
            data:formdata,
            #下面两个配置项必须加,他们和数据类型的处理有关
            processData: false ,    // 不处理数据
            contentType: false,    // 不设置内容类型

            success:function(data){
                console.log(data)
            }
        })

    })
    
</script>
</body>
</html>
 
20155953-c2b8cdf6362280eb.png
页面截图
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒 暄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值