创建一个简单的HTML文件上传页面 [英] Creation of a simple HTML file upload page
问题描述
我想要的是最简单的HTML网页来处理上传和保存文件名称我指定。
我试图用这个:
< ; form action =../ cgi-bin / upload.pymethod =postenctype =multipart / form-data>
< input type =filename =upload/>
< input type =submit/>< / form>
在 upload.py
:
$ p $
#!/ usr / bin / python
导入os
导入命令
导入cgi,cgitb
cgitb.enable()
printContent-Type:text / html
print
print'start!'
form = cgi.FieldStorage()
filedata = form ['upload']
不知道如何将其保存在文件中,如Beautiful.mp3。
可以有任何帮助吗?
虽然,我不想使用任何脚本。我只想要最基本的HTML页面。只有当必须有一些CGI处理程序时,Python脚本才会存在。 Flash不是首选。
filedata
类似于普通文件的对象。基本上你会这样做:
pre $ 如果filedata.file:#字段确实是一个上传
与文件(美丽.mp3,'w')作为outfile:
outfile.write(filedata.file.read())
或者,您可以使用 read()
, readlines()
或 readline()
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()
这篇关于创建一个简单的HTML文件上传页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!