When you upload file with Django, the response won't be returned until the file upload has completed. If the uploaded file is large, it will take a long time, during which the user can't do anything but wait. Is there any way to implement asynchronous processing of file uploading? So, when a file is uploading backend, the user can do some other operation on the current page without interrupting the upload?
Django has support for writing asynchronous (“async”) views, along with an entirely async-enabled request stack if you are running under ASGI. Async views will still work under WSGI, but with performance penalties, and without the ability to have efficient long-running requests.
The Django documentation for File Uploads, gives a brief understanding of the workflow of how to upload a single file using forms and importing in views. In a similar way, we can also tweak it to upload multiple files using forms and views.
An async view function in Django is detected by the annotation async def , which then runs the async view in a thread within its own event loop. This gives the benefit of being able to do and run tasks concurrently inside the async views.
Django provides built-in library and methods that help to upload a file to the server. The forms. FileField() method is used to create a file input and submit the file to the server. While working with files, make sure the HTML form tag contains enctype="multipart/form-data" property.
I am aware that it has been more that 5 years since this question was asked but I have similar problem and there are no "simple answers" on SO.
For your problem I may suggest using a progress bar (if using Django forms). Uploading a file asynchronously might not be possible in Django.
In my case the browser element is not crucial and I am considering moving the file upload from browser upload to some sort of FTP / AWS S3 file storage and working on this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With