Article From:

Referring to the Django documentation, I wrote a multi-file upload view. py, but the test can only be saved to the last file, part of the code is as follows:

def test_5(request,template='test_5.html'):
    if request.method=='POST':
        a = request.FILES.getlist('file')
        print a
        for files in request.FILES.getlist('file'):
            print files
            form = UploadFileForm(request.POST, request.FILES)
            print form
            if form.is_valid():
                f = handle_uploaded_file(request.FILES['file'])
                print f
                title = f[0]
                path = f[1].decode('gbk')
                upload = list()
                upload.append(uploading(filetitle=title, filepath=path))
        return HttpResponse('Upload successful ")Else:Form = UploadFileForm ()Context = {{'form': form,}RetUrn render (request,'test_5.html', context)

To facilitate debugging, I added a lot of print, the front-end uploaded three files AAAAAAAAAA. jpg, BBBBBBBBB. jpg, CCCCCCCCC. GIF to do the test, the output is as follows:

[<InMemoryUploadedFile: AAAAAAAA.jpg (image/jpeg)>, <InMemoryUploadedFile: BBBBBBBBBBB.jpg (image/jpeg)>, <InMemoryUploadedFile: CCCCCCCCCCC.gif (image/gif)>]
<tr><th><label for="id_file">File:</label></th><td><input type="file" name="file" required id="id_file" /></td></tr>
(u'CCCCCCCCCCC.gif', 'F:\\django_test\\media\\')
<tr><th><label for="id_file">File:</label></th><td><input type="file" name="file" required id="id_file" /></td></tr>
(u'CCCCCCCCCCC.gif', 'F:\\django_test\\media\\')
<tr><th><label for="id_file">File:</label></th><td><input type="file" name="file" required id="id_file" /></td></tr>
(u'CCCCCCCCCCC.gif', 'F:\\django_test\\media\\')

Why is it last saved to CCCCCCCCCCC.gif?
Upload file processing:

def handle_uploaded_file(f):
        path = os.path.join(BASE_DIR, 'media\\')
        if not os.path.exists(path):
            file_name = str(path +
            destination = open(file_name, 'wb+')
            for chunk in f.chunks():
    except Exception, e:
        print e
    return, path

The above is my question. It is the problem that goes through the uploading of files.

Answer 0:

Debugged today, the problem lies inf = handle_uploaded_file(request.FILES['file'])Above,request.FILES['file']Contains three files that were traversed before.for afile in request.FILES.getlist('file')The afile inside is a separate stream file. It only needs to be changed intof = handle_uploaded_file(afile)Just fine.

Answer 1:

Lie inf = handle_uploaded_file(request.FILES['file']) thisrequest.FILES['file'] It’s the last one. You know it.getlist Now, we should know how to deal with it.

Answer 2:
f = handle_uploaded_file(request.FILES['file'])
print f  // =[<InMemoryUploadedFile: AAAAAAAA.jpg (image/jpeg)>, <InMemoryUploadedFile: BBBBBBBBBBB.jpg (image/jpeg)>, <InMemoryUploadedFile: CCCCCCCCCCC.gif (image/gif)>]

Obviously, your f is a plural form, and you just write in a file.

I wonder if I can take request.FILES[‘file’][0] like this.
If you do not report indexerror

Then you can.

for i in range(len(request.FILES['file'])):
    f = request.FILES['file'][i]

But there seems to be a problem with this. It’s like I couldn’t get the name f. I’ve had this problem before.

Leave a Reply

Your email address will not be published. Required fields are marked *