doudiecai1572 2018-05-21 09:45
浏览 33
已采纳

SilverStripe 4 - 没有上传文件/通过AJAX写入数据库

as reported here, I'm stuck in an issue related to files uploaded on CMS by ajax request. Basically, I got a front-end form that must send files via ajax calls to a page controller. The files must be saved and related to a Member/DataExtension that implements a File::class/$many_many property/relation (in order to save and associate them to every CMS user).

Here's my approach:

My 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',
        [...]
    ];
[...]

The Controller involved:

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');
                }
            }
        }
    }
}

This results in no DB File table written as well as no file uploaded in the designed assets destination folder. Furthermore, no excetptions are thrown - it returns the HTTPResponse so I presume that the code beign run with no errors?. In any case, here's the request result:

Headers Payload

At this time I don't understand what I'm missing here.

Can anyone help me to catch the error?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • douhao2548 2018-05-23 07:05
    关注

    Seems that my problem was related to a wrongly injected dependency:

    use SilverStripe\Security;
    

    By changing it to point to the correct controller:

    use SilverStripe\Security\Security;
    

    the files start to beign uploaded as expected.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误