0

我不知道为什么,但是当我将文件发送到服务器时,当前页面会同时重新加载...... Postman 也会发生这种情况。

我使用官方文档的副本。

我使用 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);
    }

  }
4

0 回答 0