zoukankan      html  css  js  c++  java
  • Django(十七)文件上传

    http://www.cnblogs.com/wupeiqi/articles/5703697.html


        - 文件上传
            - 普通上传
            - 自定义页面上传按钮
            - 基于Form做上传
            - Ajax上传文件?????

    一,上传文件

    from django.shortcuts import render,redirect, HttpResponse
    
    # Create your views here.
    
    def update(request):
        if request.method == 'GET':
            return render(request, 'update.html')
        else:
            username = request.POST.get('username')
            img = request.FILES.get('img')
            print(img)
            print(username)
            file_name = img.name
            file_size = img.size
            print(file_name, file_size)
            f = open(img.name, 'wb')
            for line in img.chunks():
                f.write(line)
            f.close()
    
    
            return HttpResponse("ok")
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
    <form action="/update/" method="post" enctype="multipart/form-data">
        {% csrf_token %}
    
        <input type="text" name="username">
        <div style="position: relative">
            <a href="">上传</a>
           <input type="file" name="img" style="opacity:0; position: absolute;left: 0;top: 0;">
        </div>
    
        <input type="submit" value="提交" />
    </form>
    
    </body>
    </html>

    基于Form上传

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
    <form action="/update/" method="post" enctype="multipart/form-data">
        {% csrf_token %}
    
        {{ obj.user }}
        {{ obj.img }}
    
        <input type="submit" value="提交" />
    </form>
    
    </body>
    </html>
    from django.shortcuts import render,redirect, HttpResponse
    
    # Create your views here.
    from django import  forms
    from django.forms import fields
    class Upload(forms.Form):
        user = fields.CharField()
        img = fields.FileField()
    
    def update(request):
        if request.method == 'GET':
            obj = Upload()
            return render(request, 'update.html',{'obj':obj})
        else:
            obj = Upload(request.POST, request.FILES)
            if obj.is_valid():
                username = obj.cleaned_data['user']
                img = obj.cleaned_data['img']
                print(img)
                print(username)
                file_name = img.name
                file_size = img.size
                print(file_name, file_size)
                f = open(img.name, 'wb')
                for line in img.chunks():
                    f.write(line)
                f.close()
    
    
            return HttpResponse("ok")
  • 相关阅读:
    HttpModule &amp; HttpHandler
    试图运行项目时出错:无法启动调试 没有正确安装调试器
    DataTable 手动排序 无效的问题解决方法??
    合并多个DLL文件
    恢复mysql方法
    在不同版本的IIS下使用ASP.NET MVC
    Enterprise Library 4.1 October 2008
    去除HTML标记
    Mac相关命令
    Mac常用快捷键
  • 原文地址:https://www.cnblogs.com/xiangtingshen/p/10686712.html
Copyright © 2011-2022 走看看