【问题标题】:DreamFactory: How to upload an image to the fileserver and save the path of the image in DataBase?DreamFactory:如何将图像上传到文件服务器并将图像的路径保存在数据库中?
【发布时间】:2016-02-16 13:13:04
【问题描述】:

我正在考虑使用 DreamFactory 作为 API。我对服务器逻辑很陌生。我想将图像存储在文件系统上,并将存储文件的路径存储到数据库中。我不会将图像作为 blob 存储在数据库中,因为它们后面还会有许多其他图像。有人可以指出我正确的方向吗?

首先使用以下方法将图像发布到文件系统:

post /files/{file_path} 

然后:

post /mysql/_table/{table_name} createRecords() - Create one or more records

但是如何将路径传递给数据库呢?

【问题讨论】:

    标签: database post save store dreamfactory


    【解决方案1】:

    一种方法是在 POST 到 /files/{file_path} 时使用服务器端脚本(在本例中为 V8J)。转到 Admin 应用程序的 Scripts 选项卡,然后按自己的方式处理事件到以下路径...

    Process Event Scripts > files > files.{file_path} > [POST] files.{file_path} > files.{file_path}.post.post_process
    

    试试下面的脚本...

    // event.resource is the {file_path} part, 
    // including any folders from root, i.e. my/path/file.txt
    var_dump(event.resource); 
    
    // Create a record that you want to post to a table, 
    // using the "todo" table for example, setting "name" to the file path
    // "mysql" is my db service name
    var record = {"resource": [ {"name": event.resource, "complete": false } ] }; 
    var result = platform.api.post('mysql/_table/todo', record);
    
    var_dump(result); 
    // result contains a resource array with the id of the record created
    // like {"resource": [ {"id": 5} ] }
    

    【讨论】:

    • 非常感谢,只是为了理解:脚本在执行 POST 文件后自动触发。{file_path}(还不能测试)?第二个:它会被整个安装覆盖,不是吗(我的意思是,我可以将此脚本仅用于一个应用程序)吗?
    • 是的,事件以及应用于该事件的任何活动脚本都会自动触发,在服务处理 POST 之前“pre_”和之后“post_”。第二个:是的,但是您可以通过使用 event.request.headers 'x-dreamfactory-api-key' (或根据您传递密钥的方式使用参数 'api_key' )来检查适用于特定应用程序的 api_key。
    • 我遇到了另一个问题:我尝试了这个脚本,但它从未被触发。这是一个已知的错误,还是我错过了什么?我启用了错误日志记录,它在自定义脚本中运行良好,但不适用于脚本选项卡中的脚本。
    • 更新:它只是不适用于文件存储的脚本。对 mysql .get.post_process 进行了同样的尝试,它在那里工作。
    • 更新 2:只有 v8js 不工作。我用php重写了这个,数据写入了数据库。如果尝试您的代码,错误是:语法错误:; .
    猜你喜欢
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 2012-01-18
    • 2017-09-25
    相关资源
    最近更新 更多