ng2-file-upload对php有用吗?

时间:2017-05-01 11:42:53

标签: php angular ng2-file-upload

我不知道为什么,但是当我将文件发送到服务器时,当前页面会在同一时间重新加载......这也发生在邮递员身上。

我使用官方文档的副本。

我使用SlimFramework:

路线:

$app->post('/api/file/upload', FileController::class . ':add');

控制器:

public function add (Request $request, Response $response, $args) {

        $imagepath = '';
        $files = $request->getUploadedFiles();

        $newfile = $files['file'];

        if($newfile->getError() === UPLOAD_ERR_OK) {
            $uploadFilename = $newfile->getClientFilename();
            $newfile->moveTo('../public/src/assets/img/' . $uploadFilename);
            $imagepath = "assets/img/" . $uploadFilename;
        }

        $addIconQuery = 'UPDATE categories SET icon = :icon WHERE id_category = :id';

        $stmt = $this->container->get('db')->prepare($addIconQuery);

        $stmt->execute([
            ':id'   => 21,
            ':icon' => $imagepath
        ]);

    }

文件已正确上传,路径已添加到数据库中。

有什么想法吗?

更新

<div class="field">
    <input type="file" name="file" ng2FileSelect [uploader]="uploader">
       <button type="button" class="btn btn-success btn-xs" (click)="uploader.uploadAll()">
            <span class="glyphicon glyphicon-upload"></span> Upload
    </button>
</div>

在组件中

public uploader: FileUploader = new FileUploader({ url: Config.apiUrl + 'api/file/upload' });

ngOnInit() {
    this.uploader.onAfterAddingFile = (file) => { file.withCredentials = false; };

    this.uploader.onCancelItem = (item:any, response: any, status: any, header:any) => {
      console.log('ImageUpload:uploaded:', item, status, response);
    }

  }

0 个答案:

没有答案