Django Python не может загрузить каталог с большим количеством файлов - PullRequest
0 голосов
/ 30 мая 2020

Я пытаюсь загрузить каталог с большим количеством файлов dicom, используя Django и JQuery ajax. Размер каждого файла не более 600 Кб. Я могу загружать 200 файлов за раз. Если я увеличил количество файлов (попытался загрузить 14000 файлов), это не сработает. Сайт зависает и не показывает никаких ошибок. Может ли кто-нибудь помочь мне с этой проблемой? Я прикрепил свой код ниже. Заранее благодарим.

View.py:

def handle_uploaded_file(f,filePath):
  with open(filePath+'/'+f.name, 'wb+') as destination:
    for chunk in f.chunks():
        destination.write(chunk)

def UploadScanView(request):
  if request.method == 'POST':
     form = create_scan_form(request.POST)
     if form.is_valid():
        scan = form.save(commit=False)
        scan.project_id = form.cleaned_data.get('project_id')

        directory_name = request.POST.get('directories')
        json_to_dictionary = json.loads(directory_name)
        print(json_to_dictionary)
        for upload_file in request.FILES.getlist('file'):
            file_path = settings.MEDIA_ROOT+'/'+os.path.dirname(json_to_dictionary[upload_file.name])
            print(file_path)
            if os.path.exists(file_path):
                handle_uploaded_file(upload_file, file_path)
            else:
                os.makedirs(file_path)
                handle_uploaded_file(upload_file,file_path)
         return render(request, 'fileupload/basic_upload/scan_upload.html', {'form': form})
  else:
    form = create_scan_form()  
  return render(request, 'fileupload/basic_upload/scan_upload.html', {'form': form})

HTML и JQuery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>

    <script>
        /*fileupload div*/
        $(document).ready(function(){
          $("#my-file").on('change',function(e){ //submit(function(e){
              $("#file-wrap p").text('Now click on Upload button');
            });
          $("#my-form").on('submit',function(e){ //submit(function(e){
             files = document.querySelector("#my-file").files;
             var directories = {}
                for (var file of files) {
                    file.webkitRelativePath
                    directories[file.name] = file.webkitRelativePath
                }
            directories = JSON.stringify(directories);
            document.querySelector("#directories").value = directories
            var eventType = $(this).attr("method"); // get method type for #my-form
            var eventLink = $(this).attr("action"); // get action link for #my-form
            //alert(directories);
                //////
            var formData = new FormData(this);
            formData.append('csrfmiddlewaretoken', '{{ csrf_token }}');
            $.ajax({
                    headers: { "X-CSRFToken": '{{ csrf_token }}' },
                    type: eventType,
                    url: eventLink,
                    //data: new FormData(this), // IMPORTANt
                    data: formData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    // this part is progress bar
                    xhr: function () {
                            var xhr = new window.XMLHttpRequest();
                            xhr.upload.addEventListener("progress", function (e) {
                                if (e.lengthComputable) {
                                    var percentComplete = e.loaded / e.total;
                                    percentComplete = parseInt(percentComplete * 100);
                                    $('.myprogress').text(percentComplete + '%');
                                    $('.myprogress').css('width', percentComplete + '%');
                                }
                            }, false);
                            return xhr;
                        },
                    success: function(getResult) {
                        $('#my-form')[0].reset(); // reset form
                        $("#file-wrap p").html('Drag and drop file here'); // change wrap message
                        }
                });
                e.preventDefault();
        });
    });

        /*fileupload div*/

    </script>
<form id="my-form" method="POST" action="{% url 'fileupload:upload_scan' %}" enctype="multipart/form-data"> <!--independentSub-->
        {% csrf_token %}
        <div id="user_form" class="container">
          
                <div class="form-group col-sm-4" id="project_id">
                    {{ form.project_id|as_crispy_field }}
                </div>

            <!--fileupload div-->
            <div id="independentSubDiv" class="row">
                <div id="file-wrap" class="form-group col-sm-6" >
                    <p>Drag and drop file here</p>
                    <input id="my-file" type="file" name="file" multiple webkitdirectory directory draggable="true">
                    <input type="text" id="directories" name="directories" hidden />
                </div>
            </div>

            <div style="padding-left: initial" id="independentSubDiv" class="form-group col-sm-7" >
                <button type="submit" class="btn btn-primary btn-lg btn-block" name="submit_btn" id="submit_btn">Submit</button>
            </div>
            
            <div class="progress form-group col-sm-7" style="padding-left: initial" id="progressDiv" >
                <div class="progress-bar progress-bar-success myprogress " role="progressbar">0%</div>
            </div>

       </div>
    </form>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...