在HTML表单中使用多个文件输入字段

时间:2013-04-30 11:09:50

标签: python mysql forms file-upload cgi

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

<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>

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

...
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)

抱歉代码有点乱,我没有亲,但我确实尝试

0 个答案:

没有答案