javascript 如何使用 Python/CGI 上传文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4890820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Python/CGI for file uploading
提问by seadowg
I'm trying to make a file uploader page that will prompt the user for a file and will upload while displaying progress.
我正在尝试制作一个文件上传页面,该页面将提示用户输入文件并在显示进度的同时上传。
At the moment I've managed to make a simple HTML page that can calls my python script. The python script will then get the file and upload in 1000 byte chunks.
目前我已经设法制作了一个可以调用我的 python 脚本的简单 HTML 页面。然后,python 脚本将获取文件并以 1000 字节的块上传。
I have two main problem (mainly due to be completely new to this):
我有两个主要问题(主要是因为对此完全陌生):
1) I can't get the file size to calculate percentage 2) I don't know how to communicate between the server side python and whatever is in the page to update the progress status;presumably javascript.
1) 我无法获得文件大小来计算百分比 2) 我不知道如何在服务器端 python 和页面中的任何内容之间进行通信以更新进度状态;大概是 javascript。
Am I going about everything the wrong way? Or is there a solution to my woes?
我是否以错误的方式处理所有事情?或者有没有办法解决我的困境?
Here is my python code:
这是我的python代码:
#!/usr/local/bin/python2.5
import cgi, os
import cgitb; cgitb.enable()
try:
import msvcrt
msvcrt.setmode (0, os.O_BINARY)
msvcrt.setmode (1, os.O_BINARY)
except ImportError:
pass
form = cgi.FieldStorage()
upload = form['file']
if upload.filename:
name = os.path.basename(upload.filename)
out = open('/home/oetzi/webapps/py/' + name, 'wb', 1000)
message = "The file '" + name + "' was uploaded successfully"
while True:
packet = upload.file.read(1000)
if not packet:
break
out.write(packet)
out.close()
else:
message = "Derp... could you try that again please?"
print """\
Content-Type: text/html\n
<html><body>
<p>%s</p>
</body></html>
""" % (message,)
采纳答案by scoffey
This is more complex than it seems, given how file uploading works in the HTTP protocol. Most web servers will give control to the CGI script only when the uploaded file has been completely transferred, so there's no way to give feedback in the meanwhile.
考虑到文件上传在 HTTP 协议中的工作方式,这比看起来更复杂。大多数网络服务器只有在上传的文件完全传输后才会将控制权交给 CGI 脚本,因此在此期间无法提供反馈。
There are some Python libraries that attempt to tackle this issue, though. For example: gp.fileupload(works with WSGI, not CGI).
不过,有一些 Python 库试图解决这个问题。例如:gp.fileupload(适用于 WSGI,而不是 CGI)。
The trick is to provide a way to query the upload progress via AJAX while still transferring the uploaded file. This is of no use if the web server (for example, Apache or nginx) is not configured to support the upload progress feature because you will probably see a 0% to 100% jump in the progress bar.
诀窍是提供一种通过 AJAX 查询上传进度的方法,同时仍然传输上传的文件。如果 Web 服务器(例如 Apache 或 nginx)未配置为支持上传进度功能,则这没有用,因为您可能会在进度条中看到 0% 到 100% 的跳跃。
I suggest you try Plupload, which works on the client-side and is much simpler.
我建议您尝试Plupload,它在客户端工作并且简单得多。

