2017-05-16 55 views
2

我想上傳一個圖像到我的數據庫作爲blob(我知道它的壞習慣,但我必須)。我不斷收到錯誤是Call to a member function openFile() on nullAngular2錯誤嘗試上傳圖像到Lumen後端

我的形式

<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> 

我的控制器在流明

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' => '[email protected]' 
     ]); 
    }); 

回答

2

你有具體的內容類型爲「多/ 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>