我想要一种更简单的方法来从手机中取出文件,而不是取出 SD 卡,我想只需让 CGI 程序在 Web 服务器上接收它们,所以我也有同样的问题。我编写了一个小脚本,可以使用 Web 浏览器从远程计算机成功上传文件。
它看起来像这样:
#!/usr/bin/python
import os
import cgi
def tag(tag, contents=None, attlist=None):
tagstring= "<"+tag
if attlist:
for A in attlist:
V= attlist[A].replace('"','"')
attstring= ' '+A+'="'+V+'"'
tagstring += attstring
if contents:
tagstring += ">\n"+contents.rstrip()+"\n</"+tag+">\n"
else:
tagstring += "/>\n"
return tagstring
content_type= 'Content-type: text/html\n\n'
form = cgi.FieldStorage()
if not form:
acturl= "./up.py"
ff= tag('input','',{'type':'file','name':'filename'}) + tag('input',''{'type':'submit'})
f= tag('form',ff, {'action':acturl, 'method':'POST', 'enctype':'multipart/form-data'})
H= tag('head', tag('title', "Uploader"))
B= tag('body', tag('p', f))
print content_type + tag('html', H + B)
elif form.has_key("filename"):
item = form["filename"]
if item.file:
data = item.file.read()
t= os.path.basename(item.filename)
FILE= open("/home/user/public_html/uploads/"+t,'w')
FILE.write(data)
FILE.close
msg= "Success! "
else:
msg= "Fail. "
H= tag('head', tag('title', "Uploader"))
B= tag('body', tag('p', msg + tag('a','Another?',{'href':'./up.py'})))
print content_type + tag('html', H + B)
使用这样的程序运行测试是了解您的品牌的手机浏览器是否符合您的要求的唯一可靠方法,但对我来说:它有效。我什至可以使用 Apache mod_auth 来要求用户名和密码,Android 浏览器礼貌地让我输入。然后,当我选择选择文件按钮时,它会弹出一个菜单让我从画廊、音乐应用程序、录音机和我安装的文件管理器应用程序中进行选择。我从图库中挑选了一个文件,虽然花了很长时间,但它上传得很好。所以对我来说这个问题的答案是“是”。对你来说 - 尝试一个像发布的那样的测试程序。