我使用jQuery Form Plugin上传文件。这些文件被发送到我的cherrypy脚本并返回到我的jQuery,然后文件名被附加到我的页面。系统在localhost上运行正常。我使用webfaction作为webhost,当我尝试使用表单插件上传文件时,我的jQuery错误日志中出现以下错误:
2013/11/24 16:41:26 [错误] 26628#0:* 2912993 open()“/ home / mywebsite / webapps / htdocs / submit”失败(2:没有这样的文件或目录),客户端: 5.100.131.14,server:mywebsite.webfactional.com,request:“POST / submit HTTP / 1.1”,host:“mywebsite.webfactional.com”,referrer:“http://mywebsite.webfactional.com/freelinreg”
奇怪的是它试图在“/ home / mywebsite / webapps / htdocs / submit”中打开提交文件,但不存在。根据我的下面的代码,通常看起来像cherrypy,在运行我的电脑时,在“http://mywebsite.webfactional.com/freelinreg/submit”或“localhost:8080 / submit”上可以使用'/ submit'。
是否有人指出jQuery表单插件在“http://mywebsite.webfactional.com/freelinreg/submit”而不是“/ home / mywebsite / webapps / htdocs / submit”中查找“/ submit”?
类Root(对象):
@cherrypy.expose
def index(self)
return open('/home/joestox/webapps/freelinreg_static/index.html')
@cherrypy.expose
def submit(self, myfile):
cherrypy.session['myfile'] = myfile
data_name = myfile.filename
#Send back to JQuery with Ajax
#Put in JSON form
data_name= json.dumps(dict(title = data_name))
cherrypy.response.headers['Content-Type'] = 'application/json'
return data_name
HTML:
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='freelinreg_static/google.js'></script>
<script type='text/javascript' src='freelinreg_static/frontend.js'></script>
<script type='text/javascript' src='freelinreg_static/malsup.js'></script>
</head>
<body>
<form id="dataform" action="submit" method="post" enctype="multipart/form-data">
<input type="file" name="myfile" id="myFile"/>
<input type="submit" id="data_submit" value="Continue"/>
</form>
</body>
</html>
jQuery(frontend.js):
$(document).ready(function () {
(function () {
$('#dataform').ajaxForm({
success: function (data) {
var $a_var = data['title'];
$('body').append($a_var);
}
});
return false;
})();
});
答案 0 :(得分:0)
@cherrypy.expose
def submit(self, myfile):
cherrypy.session['myfile'] = myfile
data_name = myfile.filename
upload_path = '/home/mywebsite/webapps/submit' + myfile.filename
size = 0
all_data = bytearray()
while True:
data = myfile.file.read(8192)
all_data += data
if not data:
break
size += len(data)
saved_file=open(upload_path, 'wb')
saved_file.write(all_data)
saved_file.close()
#Send back to JQuery with Ajax
#Put in JSON form
data_name= json.dumps(dict(title = data_name))
return data_name
此外,您希望用户能够查看该文件。如果是这种情况,您还需要为cherrypy打开静态浏览(如果您没有提供静态内容的Web服务器)。
cherrypy.config.update({'tools.staticdir.on': True,
'tools.staticdir.dir': '/home/mywebsite/webapps/submit'
})
希望这有帮助!