Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 upload big file in Django with process bar and i get error : MemoryError
i create a web page in Django for upload big file (any file) with process bar

i used IIS and django version 2.2.2 :
def upload_file(request):
if request.method == 'POST':
    form = UploadFileForm(request.POST, request.FILES)
    if form.is_valid():
        return HttpResponse('/success/url/')
    form = UploadFileForm()
return render(request, 'Demo/home.html', {'form': form})

def handle_uploaded_file(f):
 namefile =
 with open(namefile, 'wb+') as destination:
    for chunk in f.chunks():
from django import forms

class UploadFileForm(forms.Form):
 title = forms.CharField(max_length=50)
 file = forms.FileField()
upload.js :
   $(document).ready(function () {

    $('form').on('submit', function (event) {


        var formData = new FormData($('form')[0]);

            xhr: function () {
                var xhr = new window.XMLHttpRequest();

                xhr.upload.addEventListener('progress', function (e) {

                    if (e.lengthComputable) {

                        console.log('Bytes Loaded: ' + e.loaded);
                        console.log('Total Size: ' +;
                        console.log('Percentage Uploaded: ' + (e.loaded /

                        var percent = Math.round((e.loaded / * 100);

                        $('#progressBar').attr('aria-valuenow', percent).css('width', percent + '%').text(percent + '%');



                return xhr;
            type: 'POST',
            url: '/',
            data: formData,
            processData: false,
            contentType: false,
            success: function () {
                alert('File uploaded!');


<!DOCTYPE html>
<html lang="en">

    {% load static %}
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src=""></script>
    <script src="{% static 'Demo/upload.js' %}"></script>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href=""
        integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

    <!-- Optional theme -->
    <link rel="stylesheet" href=""
        integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

    <!-- Latest compiled and minified JavaScript -->
    <script src=""
        integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous">


        <form action="" method="post" enctype="multipart/form-data">
            {% csrf_token %}
            <input type="submit" value="ok">

        <div class="progress">
            <div id="progressBar" class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0"
                aria-valuemax="100" style="width: 0%;">

i get this error :
Quote:Internal Server Error: / Traceback (most recent call last): File "C:\Python37\lib\site-packages\django\core\handlers\", line 34, in inner response = get_response(request) File "C:\Python37\lib\site-packages\django\core\handlers\", line 106, in _get_response response = middleware_method(request, callback, callback_args, callback_kwargs) File "C:\Python37\lib\site-packages\django\middleware\", line 295, in process_view request_csrf_token = request.POST.get('csrfmiddlewaretoken', '') File "C:\Python37\lib\site-packages\django\core\handlers\", line 110, in _get_post self._load_post_and_files() File "C:\Python37\lib\site-packages\django\http\", line 314, in _load_post_and_files self._post, self._files = self.parse_file_upload(self.META, data) File "C:\Python37\lib\site-packages\django\http\", line 274, in parse_file_upload return parser.parse() File "C:\Python37\lib\site-packages\django\http\", line 254, in parse chunk = handler.receive_data_chunk(chunk, counters[i]) File "C:\Python37\lib\site-packages\django\core\files\", line 174, in receive_data_chunk self.file.write(raw_data) MemoryError

in i add:


what is my mistake .. is better way for upload big file with process bar in django
I'm not familiar with Django.

Maybe the print(f.chunks()) causes the MemoryError.
Normally it should not, because the print function does not consume generators.

In the documentation they write, that the method UploadedFile.chunks() should used always.
It the method returns a generator, which is consumed by the for-loop.
The for-loop itself should not cause this issue. For each iteration you get a new chunk
and the old chunk object is garbage collected.

Maybe you overwrite at some other place your settings. Does it work with small uploads?
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
Hi tanks for your answer

I don't know what is problems it is work for small file upload 50 mb but for 700 mb i tested my code and give me error "memory error "
As DeaD_EyE sad, "print" function can be the reason of troubles, especially in Windows (you use Windows as I guess seeing IIS).
I've run the same code on Windows 10 Pro core I7 64GB, And Linux virtual machine.

import timeit
print(timeit.timeit("print(1)", number=1000))
Now, look to results:



First of all rid of print. It gives you terrible overhead in any case.
Second, try your code on the usually recommended environment: Linux + uwsgi + Nginx. I can't except, that IIS is the trouble maker.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Django Python Vscode Error Please Help Smhbugra 3 290 Jun-30-2019, 10:54 AM
Last Post: noisefloor
  Django: How to automatically substitute a variable in the admin page at Django 1.11? m0ntecr1st0 3 216 Jun-30-2019, 12:21 AM
Last Post: scidam
  Django Connection Error erfanakbari1 1 232 Mar-21-2019, 08:09 AM
Last Post: Larz60+
  Error when running .py file but not when entering exact same code in CLI Broadsworde 3 348 Dec-11-2018, 09:06 AM
Last Post: Broadsworde
  upload folder in server ftp falloff 3 545 Nov-02-2018, 09:46 PM
Last Post: Gribouillis
  How to upload images after first paragraph ends instead of uploading them at the end? SamLearnsPython 0 381 Sep-15-2018, 06:53 AM
Last Post: SamLearnsPython
  Error while creating new Creating Models on Django aniyanetworks 2 1,108 Jul-13-2018, 12:57 PM
Last Post: aniyanetworks
  django-geoposition and unknown column error? PrateekG 3 929 Jun-26-2018, 11:56 AM
Last Post: PrateekG
  Open Sqlite database file directly in django sourabhjaiswal92 0 679 May-22-2018, 05:55 AM
Last Post: sourabhjaiswal92
  python largesize(6GB) file upload fails nithya_g 2 1,039 Jan-29-2018, 09:11 PM
Last Post: micseydel

Forum Jump:

Users browsing this thread: 1 Guest(s)