代码之家  ›  专栏  ›  技术社区  ›  Luca Cattide

SilverStripe4-没有通过Ajax上载文件/写入数据库

  •  1
  • Luca Cattide  · 技术社区  · 7 年前

    按报告 here ,我陷入了一个与通过Ajax请求上传到CMS上的文件相关的问题中。 基本上,我得到了一个前端表单,它必须通过Ajax调用将文件发送到页面控制器。文件必须保存并与 Member/DataExtension 实现了一个 File::class/$many_many 属性/关系(以便将它们保存并关联到每个CMS用户)。

    我的方法是:

    我的 Member Extension

    [...]
    /**
     * Classe Utente - Estensione
     */
    class UtenteExtension extends DataExtension
    {
        // Dichiarazione Proprietà
        private static $many_many = [
            'AllegatiUpload' => File::class, // this field should save users file uploads
            [...]
        ];
        private static $owns = [
            'AllegatiUpload',
            [...]
        ];
    [...]
    

    这个 Controller 卷入的:

    use SilverStripe\Control\HTTPRequest;
    use SilverStripe\Control\HTTPResponse;
    use SilverStripe\ErrorPage;
    use SilverStripe\Assets\Folder;
    use SilverStripe\Assets\File;
    use SilverStripe\Assets\Upload;
    use SilverStripe\Security;
    use SilverStripe\Security\Group;
    use SilverStripe\Assets\Storage\AssetStore;
    
    /**
     * Classe Controller Utente - Dashboard
     */
    class UtenteDashboardController extends PageController
    {
        // Dichiarazione Proprietà
        private static $allowed_actions = [
            'carica'
        ];
    
        /**
        * Funzione gestione validazione ed invio form caricamento
        * @param  HTTPRequest $request Richiesta HTTP
        * @return HTTPResponse Risposta HTTP
        */
        public function carica(SS_HTTPRequest $request) {
            if (!$request->isAjax()) {
                return ErrorPage::response_for(404);
            } else if (!$request->isPOST()) {
                return $this->httpError(400, 'Metodo POST richiesto');
            } else {
                $documento = $request->postVar('documento'); // File data sent by FormData JS API
    
                if (!isset($documento)) {
                    return $this->httpError(500, 'Dati richiesti mancanti');
                } else {
                    // User check
                    $utente = Security::getCurrentUser();
    
                    if ($utente->inGroup(Group::get()->filter('Code', 'clienti')->first()->ID)) {
                        // File uploading
                        $cartellaUpload = 'clienti/'. strtolower($utente->Surname) .'/uploads';
    
                        Folder::find_or_make($cartellaUpload);
    
                        // I tried with the official guide approach, with this line - with no results
                        //$utente->AllegatiUpload()->setFromLocalFile($cartellaUpload, documento, AssetStore::CONFLICT_OVERWRITE);
                         // Then I tried with this approach
                        $file = File::create();
                        $upload = Upload::create();
    
                        $upload->loadIntoFile($documento, $file, $cartellaUpload);
    
                        // Upload check
                        if ($upload->isError()) {
                            return $this->httpError(400, 'Errore di caricamento file');
                        } else {
                            // Relate the file to the destinaion user field
                            $utente->{'AllegatiUpload.ID'} = $upload->getFile()->ID;
                            // Update user
                            $utente->write(); 
    
                            return new HTTPResponse;
                        }
                    } else {
                        return $this->httpError(401, 'Utente non abilitato');
                    }
                }
            }
        }
    }
    

    这将导致没有写入数据库文件表,也不会在设计的 assets 目标文件夹。此外,不会引发任何异常-它返回 HTTPResponse 所以我假设代码运行时没有错误?。在任何情况下,这是请求结果:

    Headers Payload

    现在我不明白我在这里错过了什么。

    有人能帮我找出错误吗?

    事先谢谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Luca Cattide    7 年前

    似乎我的问题与错误注入的依赖有关:

    use SilverStripe\Security;
    

    通过将其更改为指向正确的控制器:

    use SilverStripe\Security\Security;
    

    文件开始按预期上传。