Python File Upload

Source: Internet
Author: User

Submission based on form form forms
# You   need to specify Form-data, you cannot directly spell-key-value pairs

You can specify where the name photo exists

views.py

 fromDjango.shortcutsImportRender,redirect,httpresponse#Create your views here.ImportOSdefIndex (Request):ifrequest.method=="POST":        Print(Request. POST)#only key value pairs are placed in post #< querydict: {' Csrfmiddlewaretoken ':#[' hs5bynikbnqazl7svwkxwrjxxy5sftir1mxrmumqox9dizs4bwz7nn183um8h0u8 '], ' user ': [' 6666 '], ' f ': [' vzh.jpg ']} >         Print(Request. FILES)#<multivaluedict: {' F ': [<inmemoryuploadedfile:vzh.jpg (image/jpeg) >]}>obj = Request. Files.get ("F")#remove the object that uploaded the fileName = Obj.name#the name of the specific fileF_write = open (Os.path.join ("APP01","Static","img", name),"WB")#If you want to place the file in a specific location, stitch the path together, plus the name                                    #If you put only name, then the file will exist under the current root directory         forLineinchObj:#Take out data for each rowF_write.write (line)returnHttpResponse ("111")    returnRender (Request,"index.html")defput (Request):returnHttpResponse ("OK")
<! DOCTYPE html>"en">"UTF-8"> <meta http-equiv="x-ua-compatible"Content="Ie=edge"> <meta name="Viewport"Content="Width=device-width, initial-scale=1"> <title>Title</title>{#form-data type can upload file #}<form action=""Method="Post"Enctype="Multipart/form-data">    {% Csrf_token%}    <p><input type="text"Name="User"></p> <p><input type="file"Name="F"></p> <input type="Submit"></form></body>index.html

Note the point:

Form form <form action= "" method= "post" enctype="  Multipart/form-data"> need to add enctype="multipart/form-data  "form-data can be used to upload files

Python File Upload

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.