简体   繁体   中英

Creation of a simple HTML file upload page

First, I don't need any functions more than upload a file. No progress bar, no file type, or size check, no multiple files.

What I want is the most simple HTML webpage to handle the upload and save the file with the name I specified.

I tried to use this:

<form action="../cgi-bin/upload.py" method="post" enctype="multipart/form-data">
<input type="file" name="upload" />
<input type="submit" /></form>

In upload.py :

#!/usr/bin/python

import os
import commands
import cgi, cgitb

cgitb.enable()
print "Content-Type: text/html"
print
print 'start!'
form = cgi.FieldStorage()
filedata = form['upload']

But I don't know how to save this in file, like "Beautiful.mp3".

Can any body help?

Though, really, I don't want to use any scripts. I just want the most basic html pages. Python scripts will only exist when there must be some CGI handlers. Flash is not preferred.

The filedata object will wrap a file-like object that can be treated like a regular file. Basically you would do this:

if filedata.file: # field really is an upload
    with file("Beautiful.mp3", 'w') as outfile:
        outfile.write(filedata.file.read())

Or, you could do just about anything else with it, using read() , readlines() or readline()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM