Angular2尝试将图像上传到Lumen后端时出错

时间:2017-05-16 19:49:39

标签: laravel angular lumen

我正在尝试将图像作为blob上传到我的数据库(我知道它的不良做法,但我必须这样做)。我一直得到的错误是Call to a member function openFile() on null

我的表格

<form ngNoForm action="http://Api.app/api/v1/uploadImage" target="_blank" method="POST">
  <input type="file" name="image" id="image">
  <input type="submit" value="Upload Image" name="submit">
</form>

我在Lumen的控制器

class ImageUploadController extends Controller {

    public function uploadImage(Request $request)
    {
        $file = $request->file('image');
        $contents = $file->openFile()->fread($file->getSize());

        $die = DB::table('paint')->find(1);

        $die->pic = $contents;
        $die->save();
    }

}

我的路线

$app->get('/', function () use ($app) {
    return $app->version();
});

$app->group(['prefix' => 'api/v1'], function($app) {
        $app->post('/uploadImage', [
            'uses' => 'ImageUploadController@uploadImage'
        ]);
    });

1 个答案:

答案 0 :(得分:2)

您必须将内容类型指定为帖子的“multipart / form-data”。

<form ngNoForm action="http://Api.app/api/v1/uploadImage" 
               target="_blank" 
               method="POST" 
               enctype="multipart/form-data">
  <input type="file" name="image" id="image">
  <input type="submit" value="Upload Image" name="submit">
</form>