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)
コードが少し乱雑で申し訳ありません。私は決してプロではありませんが、試してみます