Article From:https://segmentfault.com/q/1010000011705007
Question:

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:
views.py:

@csrf_exempt
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))
                uploading.objects.bulk_create(upload)
        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)>]
AAAAAAAA.jpg
<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\\')
BBBBBBBBBBB.jpg
<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\\')
CCCCCCCCCCC.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\\')

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

def handle_uploaded_file(f):
    BASE_DIR='F:\django_test'
    try:
        path = os.path.join(BASE_DIR, 'media\\')
        if not os.path.exists(path):
            os.makedirs(path)
        else:
            file_name = str(path + f.name)
            destination = open(file_name, 'wb+')
            for chunk in f.chunks():
                destination.write(chunk)
            destination.close()
    except Exception, e:
        print e
    return f.name, 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]
    upload(f)

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.

Similar Posts:

Leave a Reply

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