php - Laravel 5.5 - Cannot Get Dropzone Uploaded Files Into FileBag
I've been using standard HTML POST in a Laravel project for file uploads and want to us Dropzone.
I've tried looking at the documentation for Dropzone and online examples of Laravel implementation, but, I cannot get a simple file post to upload into the POST headers.
Laravel FileBag therefore remains empty and I cannot figure out why. Dumping out $_FILE results in no files being passed into the Headers. Other than this, from a browser point of view the input box for dropzone displays fine, it's responsive and doesn't display errors when a file is uploaded.
Any help would be appreciated. Code stripped down to the basics:
Master Blade:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/dropzone.css">
<script src="/dropzone.js"></script>
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
</head>
<body>
<script type="text/javascript">
var baseUrl = "{{ url('/testUpload') }}";
var token = "{{ Session::Token() }}";
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("div#dropzoneFileUpload", {
url: baseUrl,
params: {
_token: token
}
});
Dropzone.options.myAwesomeDropzone = {
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
addRemoveLinks: true,
accept: function(file, done) {
},
};
</script>
</body>
</html>
Blade:
@extends("testmaster")
<form action="/testUpload" enctype="multipart/form-data" method="post" >
{{ csrf_field() }}
<div >
<div id="dropzoneFileUpload">
</div>
</div>
<input type="submit" value="Submit">
</form>
Controller:
public function testUpload(Request $request)
{
dd($request);
}
Route:
Route::get('/test','[email protected]');
Route::post('/testUpload','[email protected]');
Resulting Request:
Request {#38 ▼
#json: null
#convertedFiles: null
#userResolver: Closure {#398 ▶}
#routeResolver: Closure {#399 ▶}
+attributes: ParameterBag {#40 ▶}
+request: ParameterBag {#39 ▶}
+query: ParameterBag {#46 ▶}
+server: ServerBag {#42 ▶}
+files: FileBag {#43 ▼
#parameters: []
}
+cookies: ParameterBag {#41 ▶}
+headers: HeaderBag {#44 ▶}
#content: null
#languages: null
#charsets: null
#encodings: null
#acceptableContentTypes: null
#pathInfo: "/testUpload"
#requestUri: "/testUpload"
#baseUrl: ""
#basePath: null
#method: "POST"
#format: null
#session: Store {#440 ▶}
#locale: null
#defaultLocale: "en"
-isHostValid: true
-isForwardedValid: true
basePath: ""
format: "html"
}
Answer
Solution:
Here's my working example:
Routes
Template
Controller
Pay attention to my Dropzone configuration, I include
X-CSRF-TOKEN
andX-Requested-With
headers. I also giveparamName
option, before I give this option, myRequest
object can't catch any file sent by Dropzone. Hope this helps.Answer
Solution:
You've used the wrong
id
while setting the options. In yourhtml
code, you've following markup:But in your
js
code you've used the following code:In this case, you should use the following instead:
Notice that, the
id
in yourhtml
isdropzoneFileUpload
notmyAwesomeDropzone
, read more.