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