Yesterday studied a day file upload progress bar implementation, toss a day is finally completed, during the encounter a variety of bugs, people can not help but tears fall.
Here, we will use the smallest amount of Ajax to implement this feature, without any browser-support detection, so it is easier to understand. Another point to note is that the background should also be involved in a foot (with), of course, very simple, as long as the confirmation of the acceptance of the file on the line (later will be explained). I'm using the Django framework to do the backstage.
There is not much to say, first on the code, the code will give a detailed explanation. First give the front end:
<!
DOCTYPE html>
Urls.py has the following 2 pattern matches
URL (r ' ^upload_page/$ ', upload_page)
url (r ' ^upload/$ ', upload)
Views.py has the following 2 request processing functions
def upload_page (Request): Return
render (request, ' upload_page.html ') #这里upload_ Page is the top-end HTML file
def upload (request):
file = Request. FILES #一定要调用上传的文件 (no matter what you do, save or do nothing, anyway, do not call on the error, it is estimated that the default does not call do not receive it. Can upload success with Ajax, or error, reason unknown return
HttpResponse ()