zoukankan      html  css  js  c++  java
  • form表单上传文件

    index.html文件 form表单中要加上 enctype="multipart/form-data"

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h4>{{ error_message }}</h4>
    <form action="/index/" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <p><input type="file" name="up_file"></p>
        <input type="submit">
    </form>
    </body>
    </html>
    
    def index(request):
        if request.method=="POST":
            file_obj=request.FILES.get("up_file")  #上传的文件从request.FILES中取
    
            f1=open(file_obj.name,"wb")
    
            for i in file_obj.chunks():
                # file.chunks() 将文件按块写入
                f1.write(i)
    
            f1.close()
    
        return render(request,"index.html",locals())
    
    

    选择文件上传,提交后,就可以在服务端后台看到所上传的文件
    可以在settings.py文件中设定上传文件的路径,或者在打开文件句柄的时候进行路径拼接来把上传的文件保存在指定的目录下

  • 相关阅读:
    window
    pages
    百度小程序 配置 app.json 文件
    JavaScript Cookie
    jQuery ajax
    jQuery ajax
    jQuery ajax
    jQuery
    jQuery
    jQuery
  • 原文地址:https://www.cnblogs.com/zhaogang0104/p/11906763.html
Copyright © 2011-2022 走看看