KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

FileUploader notification #2898

Open jernej opened this topic on on Dec 7, 2022 - 2 comments

jernej commented on Dec 7, 2022

Hi,

I have a widget that displays a file upload button and some function that is triggered on upload. But how can I then display a success note/alert when fileHandle is finished?

    protected function widgets()
    {
        return [
          Row::create([
            Panel::create()->sub([
              Row::create([
                  Inline::create([
                    FileUploader::create("import_products")
                      ->type("primary")
                      ->accept(["csv"])
                      ->notAccept(["php"])
                      ->maxFileSize(2000000)
                      ->imagePreview(true)
                      ->showDownloadLink(true)
                      ->previewWidth("150px")
                      ->previewHeight("150px")
                      ->fileHandle(function($file){
                          $name = $file["name"];
                          $filePath = $file["tmp_name"];
                          ImportProducts::import($name, $filePath);
                         // want to show note/alert to user when file Handle is complete
                      })
                      ->resolveValue(function($file){
                          return $file["name"];
                      })
                  ])
              ]),
            ]),
          ]),
        ];
    }

Thank you, Jernej

KoolReport commented on Dec 13, 2022

I have consulted dev.team, it seems not possible currently. I will mark your post as suggestion for next version.

KoolReport commented on Dec 13, 2022

A possible solution is that you create your own fileuploader derived.from our FileUploader and overwrite the actionUpload

class MyUploader extends FileUploader
{
    protected function actionUpload($request, $response)
    {
        $files = $request->files();
        if(count($files)>0) {
            $file = $files["fileuploader"];
            if($this->fileValidate($file)===true) {

                $name = $this->getFileNameWithoutExtension($file["name"]);
                $ext = $this->getFileExtension($file["name"]);
                $file["name"] = $this->fileName($name,$ext);
                if($this->fireEvent("FileHandling",["file"=>$file])) {
                    $this->fileHandle($file);
                    $this->fireEvent("FileHandled",["file"=>$file]);

                    //Successful
                    $success = true;
                }
            }
        } else {
            $this->errorMessage($this->_noFileError());
        }
        $this->update();

        //Return any alert here
        if($success) {
            return Alert::success("File is uploaded");
        }
    }
}

Hope that helps.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed
suggestion

Dashboard