[Django]-Django how to upload folder

2👍

There is newer topic which asks the same question and I have tried to answer:

Django directory upload get sub-directory names

Basically it is the default behavior of Django if you want to have different behavior you need to write your own upload handlers

0👍

I came up with easy solution for this problem.

  • You could get folder name via html and javascript in frontend
  • pass it as a value to hidden form field
  • in backend you can create a directory with that name
  • and upload files in this directory.

HTML

<input type="hidden" name="dir_name" id="id_dir_name">
<input type="file" name="file" onchange="selectFolder(event)" webkitdirectory="" multiple="" required="" directory="" id="id_file">

JS

function selectFolder(e) {
    var theFiles = e.target.files;
    var relativePath = theFiles[0].webkitRelativePath;
    var folder = relativePath.split("/");
    $("#" + id).val(folder[0]);
}

views

directory_name = form.cleaned_data['dir_name']
os.mkdir(os.path.join(settings.MEDIA_ROOT, directory_name))
handle_uploaded_file(request.FILES['file'], directory_name)

-5👍

you can use django filer

rerfer:https://github.com/stefanfoulis/django-filer

Leave a comment