1

有没有人有任何使用UserFrosting实现dropzone.js的经验?

我在浏览器的控件中收到以下错误:

POST '.../website/images/upload 400(错误请求)'

Dropzone.js被添加到 /website/images/upload 中,每当我拖放一个文件时,我都会得到上述结果。

这是我的 index.php 条目:

$app->get('/website/images/upload?', function () use ($app) {
    $controller = new RDCW\LGWebsiteController($app);
    return $controller->websiteImagesUpload();
});
$app->post('/website/images/upload?', function () use ($app) {
    $controller = new RDCW\LGWebsiteController($app);
    return $controller->imagesUpload();
});

控制器功能:

public function websiteImagesUpload()
{
    // Access-controlled page
    if (!$this->_app->user->checkAccess('uri_website')) {
        $this->_app->notFound();
    }

    $schema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/lg-file-upload.json");
    $this->_app->jsValidator->setSchema($schema);

    $this->_app->render('lg-website/image-upload.twig', [
        "validators" => $this->_app->jsValidator->rules(),
    ]);
}
public function imagesUpload()
{
    // Access-controlled page
    if (!$this->_app->user->checkAccess('uri_website')) {
        $this->_app->notFound();
    }
    $post = $this->_app->request->post();
    $ms = $this->_app->alerts;

    $requestSchema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/lg-file-upload.json");

    $rf = new \Fortress\HTTPRequestFortress($ms, $requestSchema, $post);
    $rf->sanitize();
    if (!$rf->validate()) {
        $this->_app->halt(400);
    }

    $ms->addMessageTranslated("success", "LG Notification! " . var_dump($post), $post);
}

树枝模板:

                        <form class="form-horizontal dropzone"
                              id="my-awesome-dropzone"
                              role="form"
                              name="uploadImage"
                              action="/website/images/upload"
                              method="post">
                        </form>
...
    <script>
        $(document).ready(function() {
            // Load the validator rules for this form
           var validators = {{validators | raw}};
            ufFormSubmit(
                    $("form[name='uploadImage']"),
                    validators,
                    $("#userfrosting-alerts"),
                    function(data, statusText, jqXHR) {
                        // Reload the page on success
                        window.location.reload(true);
                    }
            );
        });
    </script>

我不确定是否可以跳过架构部分,因此我包括以下内容:

{
  "file" : {
    "validators" : {
      "length" : {
        "min" : 1,
        "max" : 128,
        "message" : "Path length must be up to 128 characters long."
      }
    },
    "sanitizers" : {
      "raw" : ""
    }
  }
}

也许还有其他更好的方法来处理文件上传。我愿意接受建议:)

4

0 回答 0