RobFrancken's avatar

Remove Illuminate\Http\UploadedFile from Request object after asynchronous file upload

I have a VueJs form that uploads files. Validation is being handled by the Laravel back-end. The problem I have is that the Illuminate\Http\UploadedFile is not being removed from the request object for subsequent requests, and as such the validation for files is not working.

I have tried to reset the request parameters with $request->replace([]) after a successful upload, but on subsequent uploads (when no file has been selected) the file remains in the request object.

Successful upload:

[2019-07-23 11:01:06] local.DEBUG: array (
  'name' => 'a',
  'description' => 'a',
  'resourceType' => 'sample',
  'applicationTypes' => 
  array (
    0 => '1',
  ),
  'authorities' => 
  array (
    0 => '1',
  ),
  'file' => 
  Illuminate\Http\UploadedFile::__set_state(array(
     'test' => false,
     'originalName' => 'blueprint.pdf',
     'mimeType' => 'application/pdf',
     'error' => 0,
     'hashName' => NULL,
  )),
)

Subsequent uploads when no file has been selected

[2019-07-23 11:01:24] local.DEBUG: array (
  'name' => 'b',
  'description' => 'b',
  'resourceType' => 'sample',
  'applicationTypes' => 
  array (
    0 => '1',
  ),
  'authorities' => 
  array (
    0 => '1',
  ),
  'file' => 
  Illuminate\Http\UploadedFile::__set_state(array(
     'test' => false,
     'originalName' => 'blueprint.pdf',
     'mimeType' => 'application/pdf',
     'error' => 0,
     'hashName' => NULL,
  )),
)

Does anyone know what's causing the file to remain in the request?

0 likes
2 replies
hollyit's avatar
hollyit
Best Answer
Level 8

Sounds like the problem is actually in your Vue component. My guess is the file field isn't being cleared out. Unless you got some package or trying to do something really funky with Laravel, it should never know about other requests. That's the stateless nature of PHP. One request ends and all variables, data, etc., are gone.

EDIT: Best way to check is watch the network tab in your browser dev tools when doing the request and see exactly what is being sent to the server.

Please or to participate in this conversation.