2

在 html 表单中使用多个文件输入字段有什么技巧吗?我尝试使用其中的 3 个,以便可以一次上传 3 个文件,但是在页面上,POST只有第一个输入字段的表单实际上上传了一个文件,另外 2 个创建了 0 字节的文件。我真的不想使用带有“多个”属性的文件输入,我试图让它尽可能简单

<form enctype="multipart/form-data", action="MakeThread.py" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="15728640" />
Your Name: <input type="text" name="name" /> <br />
Thread Subject: <input type="text" name="subject" size="50"/> <br />
Upload Image: <input type="file" name="image" accept="Image/gif,Image/jpeg,Image/png" /> (Optional) <br />
Upload Image: <input type="file" name="image2" accept="Image/gif,Image/jpeg,Image/png" /> (Optional) <br />
Upload Image: <input type="file" name="image3" accept="Image/gif,Image/jpeg,Image/png" /> (Optional) <br />
Body: <textarea name="body" rows="10" cols="100"></textarea> <br />
Keywords: <input type="text" name="keywords" size="50"/> Separate keywords with a comma (funny,pictures,...)<br />
<input type="hidden" name="topic" value="%s">
<input type="submit" value="Submit">
</form>

这是后端代码,第 2 和第 3 形式被注释掉,因为我无法让它工作,但它在这里......

...
image = args['image']
#image2 = args['image2']
#image3 = args['image3']
...
if fail == False:
    filesize = str(len(image.file.read()))
#    filesize2 = str(len(image2.file.read()))
#    filesize3 = str(len(image3.file.read()))
    image.file.seek(0)
#    image2.file.seek(0)
#    image3.file.seek(0)
    debug.append(filesize)
#    debug.append(filesize2)
#    debug.append(filesize3)
    if int(filesize) > 15728640:
        stop = True
        debug.append('Image 1 is too large')
#    if int(filesize2) > 15728640:
#        stop = True
#        debug.append('Image 2 is too large')
#    if int(filesize3) > 15728640:
#        stop = True
#        debug.append('Image 3 is too large')
    fn = os.path.basename(image.filename)
#    fn2 = os.path.basename(image2.filename)
#    fn3 = os.path.basename(image3.filename)
    fnl = fn.split('.')
    ext = str(fnl[-1])
#    fnl2 = fn.split('.')
#    ext2 = str(fnl2[-1])
#    fnl3 = fn.split('.')
#    ext3 = str(fnl3[-1])
    files = 0
    if ext.endswith(('gif', 'jpg', 'jpeg', 'png')):
        fn = str(str(uuid.uuid4()) + '.' + ext)
        files = files + 1
    else:
        stop = True
        debug.append('Incorrect file type (Image 1)')
    """if ext2.endswith(('gif', 'jpg', 'jpeg', 'png')):
        fn2 = str(str(uuid.uuid4()) + '.' + ext)
        files = files + 1
    else:
        stop = True
        debug.append('Incorrect file type (Image 2)')
    if ext3.endswith(('gif', 'jpg', 'jpeg', 'png')):
        fn3 = str(str(uuid.uuid4()) + '.' + ext)
        files = files + 1
    else:
        stop = True
        debug.append('Incorrect file type (Image 3)')"""
    if stop == False:
#        if files == 3:
#            query = """INSERT INTO %s (topic_name, subject_name, poster_name, time, image, text, views, replies, keywords, image2, image3) VALUES (%%s, %%s, %%s, %%s, %%s, %%s, %%s, %%s, %%s, %%s, %%s)""" % ("Threads")
#            with con:
#                cur.execute(query, (topic, subject, postername, time, fn, body, 0, 0, formattedkeywords, fn2, fn3))
#                con.commit()
#                cur.execute('SELECT last_insert_id()')
#                pid = cur.fetchone()
#            with open("""uploadedimages/""" + fn, "wb") as fout:
#                in_chunks = iter(lambda: image.file.read(2 << 16), '')
#                fout.writelines(in_chunks)
#            with open("""uploadedimages/""" + fn2, "wb") as fout2:
#                in_chunks2 = iter(lambda: image2.file.read(2 << 16), '')
#                fout2.writelines(in_chunks)
#            with open("""uploadedimages/""" + fn3, "wb") as fout3:
#                in_chunks3 = iter(lambda: image3.file.read(2 << 16), '')
#                fout3.writelines(in_chunks)
#        elif files == 2:
#            query = """INSERT INTO %s (topic_name, subject_name, poster_name, time, image, text, views, replies, keywords, image2) VALUES (%%s, %%s, %%s, %%s, %%s, %%s, %%s, %%s, %%s, %%s)""" % ("Threads")
#            with con:
#                cur.execute(query, (topic, subject, postername, time, fn, body, 0, 0, formattedkeywords, fn2))
#                con.commit()
#                cur.execute('SELECT last_insert_id()')
#                pid = cur.fetchone()
#            with open("""uploadedimages/""" + fn, "wb") as fout:
#                in_chunks = iter(lambda: image.file.read(2 << 16), '')
#                fout.writelines(in_chunks)
#            with open("""uploadedimages/""" + fn2, "wb") as fout2:
#                in_chunks2 = iter(lambda: image2.file.read(2 << 16), '')
#                fout2.writelines(in_chunks)

抱歉代码有点乱,我无论如何都不是专业人士,但我确实尝试过

4

0 回答 0