admin管理员组文章数量:1292146
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.
I have two main problem (mainly due to be pletely new to this):
1) I can't get the file size to calculate percentage 2) I don't know how to municate between the server side python and whatever is in the page to update the progress status;presumably javascript.
Am I going about everything the wrong way? Or is there a solution to my woes?
Here is my python code:
#!/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,)
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.
I have two main problem (mainly due to be pletely new to this):
1) I can't get the file size to calculate percentage 2) I don't know how to municate between the server side python and whatever is in the page to update the progress status;presumably javascript.
Am I going about everything the wrong way? Or is there a solution to my woes?
Here is my python code:
#!/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,)
Share
Improve this question
edited Mar 6, 2013 at 20:11
bta
45.1k7 gold badges72 silver badges100 bronze badges
asked Feb 3, 2011 at 19:39
seadowgseadowg
4,2656 gold badges37 silver badges43 bronze badges
1 Answer
Reset to default 7This is more plex 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 pletely transferred, so there's no way to give feedback in the meanwhile.
There are some Python libraries that attempt to tackle this issue, though. For example: gp.fileupload (works with WSGI, not 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.
I suggest you try Plupload, which works on the client-side and is much simpler.
本文标签: javascriptHow to use PythonCGI for file uploadingStack Overflow
版权声明:本文标题:javascript - How to use PythonCGI for file uploading - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741544826a2384531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论