uploaded_file = request.FILES['my-file-1']
Why is there one-liner to copy the file from the temp location to the actual folder. I seem to be given this :
# Open the destination file in write mode
with open(file_path, 'wb') as destination_file:
# Iterate over the uploaded file chunks and write them to the destination file
for chunk in uploaded_file.chunks():
destination_file.write(chunk)
It may help if you provide more context around your question. Those two code fragments perform two different functions under two different sets of circumstances.
I have a form which has more than one file inputs where each file input is set to multiple.
I want to move / copy all those files from the temp location to a specific directory.
Ahh, that’s the issue.
See: File Uploads | Django documentation | Django
The docs even acknowledge that there’s a lot of room for improvement for Django’s handling of multiple file uploads.
How reliable is shutil
if there are like 50 file uploads ?
import shutil
shutil.copyfileobj(uploaded_file, open(file_path, 'wb'))
Also, I’m not using regular traditional form sending here.
const formData = new FormData();
const fileInputs = document.querySelectorAll('input[type="file"]');
fileInputs.forEach(fileInput =>
{
const fieldName = fileInput.name;
const files = fileInput.files;
for (let i = 0; i < files.length; i++)
{
const file = files[i];
formData.append(fieldName, file);
}
});
Then I’m using fetch to send the data as body: formData
taking into account of csrftoken
in the header. ({ headers: {'X-CSRFToken': csrftoken} }
)
I’d have no clue - I wouldn’t even try to do anything that appears as fragile as that on either end.
I think I would have to look for a file uploading service plugin that would connect the interface to the api. No to jQuery even if it exists.