【问题标题】:jQuery Form Plugin searching for page in wrong locationjQuery表单插件在错误位置搜索页面
【发布时间】:2013-12-09 05:52:27
【问题描述】:

我使用 jQuery 表单插件来上传文件。这些文件被发送到我的cherrypy脚本并返回到我的jQuery,然后文件名被附加到我的页面。系统在 localhost 上运行良好。我使用 webfaction 作为虚拟主机,当我尝试使用表单插件上传文件时,我的 jQuery 错误日志中出现以下错误:

2013/11/24 16:41:26 [错误] 26628#0: *2912993 open() "/home/mywebsite/webapps/htdocs/submit" 失败(2:没有这样的文件或目录),客户端: 5.100.131.14,服务器:mywebsite.webfactional.com,请求:“POST /submit HTTP/1.1”,主机:“mywebsite.webfactional.com”,引用者:“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”?

类根(对象):

@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;
    })();
});

【问题讨论】:

  • 您似乎混淆了本地目录和网络路径。您是否正确设置了 TMP/TEMP 环境变量?您能否提供 Python 错误转储(您可能希望为此在控制台中运行它)?

标签: jquery cherrypy jquery-forms-plugin


【解决方案1】:

jwalker 是正确的。您混淆了 url 路径和物理路径。您的 cherrypy 应用程序需要系统上的物理位置来存储文件。我也没有看到您尝试将上传的文件保存在任何地方。

@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打开静态浏览(如果您没有提供静态内容的网络服务器)。

cherrypy.config.update({'tools.staticdir.on': True,
                'tools.staticdir.dir': '/home/mywebsite/webapps/submit'
               })

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    相关资源
    最近更新 更多