将上传的图片网址传递给javascript

时间:2015-01-30 13:36:43

标签: javascript python flask

我试图将上传的照片传递给网页上的javascript,但是我收到了此错误消息。我该如何解决?

  

werkzeug.routing.BuildError

     

BuildError :(' uploaded_file',{' filename':u' user / user-1 / scan.jpeg'},无)

class AdminController(BaseController):
    route_base = ''
    route_prefix = '/admin'
    trailing_slash = True

    decorators = [login_required]

    def __init__(self):
        self.theme = "admin"

        g.theme = self.theme

        g.currentUser = g.auth.getUser()
        self.viewData = {
            "layout" : self.theme + "/" + "layouts/main.html"
        }

class BaseMethodView(MethodView):
    pass

class UserJsonDataController(AdminController, BaseMethodView):
    def __init__(self):
        super(UserJsonDataController, self).__init__()

    def uploaded_file(filename):
        return send_from_directory(app.config['UPLOAD_FOLDER'], filename) 

    def get(self):
        json = {}
        users = User.select()
        a = []
        for user in users:
            obj = {
                "user_avatar":url_for("uploaded_file", filename = user.user_avatar)
            }
            a.append(obj)
        json["rows"] = a

        return flask.jsonify(json)

module.add_url_rule('/index/show', view_func=UserJsonDataController.as_view('show'))
 $(document).ready(function () {
    $("#grid").kendoGrid({
        dataSource: {
            transport: {
                read: {
                    url: '{{ url_for("user_admin.show") }}',
                    dataType: "json"
                }
            },
            schema: {
                data: "rows"
              }
        },
        columns: [{
            field: "user_avatar",
            title: "Profil",
            template: "<img src='/#=user_avatar #' /> "    
        }
    });
});

1 个答案:

答案 0 :(得分:1)

我知道这是旧的,但为什么不呢。 @huseyin,您提到您的图片位于:app/upload/user/user-1/scan.jpeg。您的url_for()第一个参数应与该路径匹配,并且在您的代码中,它已匹配上传文件&#39;。尝试将其更改为&#39;上传&#39;