Several ways to upload files

Source: Internet
Author: User

OnePlusenctype= "Multipart/form-data"<! DOCTYPE html><title>test</title><body> <form action= "/test/" method= "POST" enctype= "Multipart/form-data" > <!--If you don't add this, Submitted data is submitted as key-value pairs- {% Csrf_token%}file: <input type= "file" name= ' Test ' ><br><input type= "Submit" value= "Submission" ></form></body>def Test (request):if Request.method = = ' POST ':file = Request. Files.get (' Test ')with open (File.name, ' WB ') as F:For line in file:F.write (line)return render (Request, ' test.html ')Two. Chunks ()def upload (request):    """before saving the uploaded file, the data needs to be stored in a certain location. By default, when the upload file is less than 2.5M, Django reads the entire contents of the uploaded file into memory. Read from memory once, write disk once. But when the upload file is large, Django writes the uploaded file to the temporary file and then stores it in the system Temp folder. :p Aram Request:: return:    """if Request.method = = "POST":# Gets the file name of the uploaded file from the requested files, which is the Name property value of the Type=files type input on the pagefilename = Request. files["File"].name# Create a new file under the project directorywith open (filename, "WB") as F:# 1.1-point read from uploaded file objectFor chunk in request. files["File"].chunks ():# Write to local fileF.write (Chunk)return HttpResponse ("Upload OK")<! DOCTYPE html><title>test</title><body><form action= "/test/" method= "POST" >{% Csrf_token%}file: <input type= "file" name= ' Test ' ><input type= "Submit" value= "Submission" ></form></body>

Several ways to upload files

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.