Django upload multiple files at once, bulk upload

Source: Internet
Author: User
Tags jquery file upload
When you upload a file with Django, from request. files[' myfiles ' gets only one file at a time, but on the HTML page it is clear that you have selected multiple files with the HTML5 file control, using the Chrome browser, which allows you to select multiple file uploads at once. In Pydev, turn on debug mode, view the Request object, and find that multiple uploaded files are actually in the request object, but why use request. files[' myfiles '] but only one?

Front HTML5 upload Control, I used jquery file upload
<span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > </span><span class= "tag" style= "font-family:arial, song body; Font-size:12px;color: #8000;" ><input</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > </span><span class= "atn" style= "font-family:arial, Song Dynasty; Font-size:12px;color: #ff00;" >type</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >=</span><span class= "ATV" style= "font-family:arial, song body; Font-size:12px;color: #00ff;" > "File" </span><span class= "PLN" style= "font-family:arial, song body; font-size:12px;" > </span><span class= "atn" style= "font-family:arial, Song Dynasty; Font-size:12px;color: #ff00;" >name</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >=</span><span class= "ATV" style= "font-family:arial, song body; Font-size:12px;color: #00ff;" > "myfiles" </span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > </span><span class= "ATN" style= "font-family:arial, song body; Font-size:12px;color: #ff00;" >multiple</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >=</span><span class= "ATV" style= "font-family:arial, song body; Font-size:12px;color: #00ff;" > "" "</span><span class=" tag "style=" font-family:arial, song body; Font-size:12px;color: #8000; >></span>




After you select multiple files, then submit the request, turn on Pydev debug mode, and view the request content:


which request. The contents of the FILES are as follows:
<span class= "Typ" style= "font-family:arial, song body; Font-size:12px;color: #2b91af;" >multivaluedict</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >:</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > </span><span class= "pun" style= "font-family:arial, song body; font-size:12px;" ><</span><span class= "Typ" style= "font-family:arial, song body; Font-size:12px;color: #2b91af;" >multivaluedict</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >:</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > </span><span class= "pun" style= "font-family:arial, song body; font-size:12px;" >{</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >u</span><span class= "str" style= "font-family:arial, Arial, Font-size:12px;color: #8000;" > ' myfiles ' </span><span class= ' pun "style=" font-family:arial, XXFarEastFont-Arial font-size:12px; >:</span><span Class= "PLN" style= "font-family:arial, song body; font-size:12px;" > </span><span class= "pun" style= "font-family:arial, song body; font-size:12px;" >[<</span><span class= "Typ" style= "font-family:arial, song body; Font-size:12px;color: #2b91af;" >inmemoryuploadedfile</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >:</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > </span><span class= "lit" style= "font-family:arial, Song Dynasty; Font-size:12px;color: #8000;" >300x600.png</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > </span><span class= "pun" style= "font-family:arial, song body; font-size:12px;" > (</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >image</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >/</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >png</span><span class= "pun" style= "font-family:arial, XXFarEastFont-font-size:12px; >) >,</span><span class= "PLN" style= "font-family:arial, song body; font-size:12px;" > </span><span class= "pun" style= "font-family:arial, song body; font-size:12px;" ><</span><span class= "Typ" style= "font-family:arial, song body; Font-size:12px;color: #2b91af;" >inmemoryuploadedfile</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >:</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > Todo</span><span class= "pun" style= "font-family:arial, song body; font-size:12px;" >.</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >txt </span><span class= "pun" style= "font-family:arial", XXFarEastFont-Arial font-size:12px; > (</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >text</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >/</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" ";p lain</span><span class= "pun" style= "font-family:arial, song body; font-size:12px;" >) >]}></span>


Obviously, the uploaded file is actually in it, but when you use request. files[' Myfiles ' is really the last one. Instead of getting two files. Trace code discovery, request. The type of files is multivaluedict, this dictionary class is a special definition, if you need to get the list need to call the GetList method, see here, the enlightened, below is to get uploaded multiple files and processing logic

<span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >files </span><span class= "pun" style= "font-family:arial", XXFarEastFont-Arial font-size:12px; >=</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > Request</span><span class= "pun" style= "font-family:arial, song body; font-size:12px;" >.</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >files</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >.</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >getlist</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" > (</span><span class= "str" style= "font-family:arial, Arial, Font-size:12px;color: #8000;" > ' myfiles ' </span><span class= ' pun "style=" font-family:arial, XXFarEastFont-Arial font-size:12px; >) </span><span class= "PLN" style= "font-family:arial, song body; font-size:12px;" > </span><span class= "KWD" style= "font-family:arial, XXFarEastFont-Font-size:12px;color: #008b; >for</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > F </span><span class= "KWD" style= "font-family:arial, Song Dynasty; Font-size:12px;color: #008b;" >in</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > Files</span><span class= "pun" style= "font-family:arial, song body; font-size:12px;" >:</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >            Destination </span><span class= "pun" Style= "Font-family:arial, XXFarEastFont-font-size:12px; >=</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > Open</span><span class= "pun" style= "font-family:arial, song body; font-size:12px;" > (</span><span class= "str" style= "font-family:arial, Arial, Font-size:12px;color: #8000;" > ' d:/temp/' </span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > </span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >+</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > F</span><span class= "pun" style= "font-family:arial, song body; font-size:12px;" >.</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >name</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >,</span><span class= "str" style= "font-family:arial, Arial, Font-size:12px;color: #8000;" > ' wb+ ' </span><span class= ' pun "style=" font-family:arial, XXFarEastFont-Arial font-size:12px; >) </span><span class= "PLN" style= "font-family:arial, song body; font-size:12px;" >      </span><span class= "KWD" style= "font-family:arial, XXFarEastFont-Arial font-size:12px;color:# 008b; " >for</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" > Chunk </span><span class= "KWD" style= "Font-family:arial", Song Dynasty; Font-size:12px;color: #008b; >in</span><spanclass= "PLN" style= "font-family:arial, song body; font-size:12px;" > F</span><span class= "pun" style= "font-family:arial, song body; font-size:12px;" >.</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >chunks</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" > (): </span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >           destination</span><span class= "pun" Style= "Font-family:arial, XXFarEastFont-font-size:12px; >.</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >write</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" > (</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >chunk</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >) </span><span class= "PLN" style= "font-family:arial, song body; font-size:12px;" > &NBSP;&NBSP;&NBSP;&NBSp Destination</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" >.</span><span class= "PLN" style= "font-family:arial, XXFarEastFont-Arial, font-size:12px;" >close</span><span class= "pun" style= "font-family:arial, Song Dynasty; font-size:12px;" > () </span>


In fact, is very simple, is not careful, another Django default is to deal with a single file upload, a one-time batch processing multiple file uploads have to use this method.
This article originates from: http://www.yihaomen.com/, original address: http://www.yihaomen.com/article/python/462.htm, thanks the original author to share.

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.