创建一个简单的HTML文件上传页面 [英] Creation of a simple HTML file upload page

查看:1995
本文介绍了创建一个简单的HTML文件上传页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我不需要比上传文件更多的功能。没有进度条,文件类型或大小检查,没有多个文件。

我想要的是最简单的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆