0

Why is it the file i upload not reflecting on the request even though the file is uploaded successfully?

HTML

<div id="upload_excel" class="dropzone form-control">

    <div class="fallback">
        <input name="file" type="file" multiple />
    </div>

</div>

JS

var baseUrl = "{{ url('/') }}";
var token = "{{ Session::getToken() }}";

Dropzone.autoDiscover = false;

var myDropzone = new Dropzone("#upload_excel", {
    paramName: "file",
    acceptedFiles: ".xls,.xlsx",
    maxFiles: 1,
    maxFilesize: 10,
    url: baseUrl + "/upload",
    params: {
        _token: token
    }
});

Controller

class UploadsController extends Controller
{
    public function upload(Request $request) {
        return $file = $request->all();
    }
}

Request Preview [enter image description here Request Response

{"_token":"ePssa9sPZxTcRR0Q4Q8EwWKjODXQ8YpCcH8H9wRP","upload_date":"2016-08-02","file":{}}

Did i miss something or what?

4
  • Well you're not actually doing anything with your file in the backend. It might just not be able to parsed into a resposne? Commented Aug 2, 2016 at 6:03
  • hmmm. so what could be the proper way so i can upload a file(specifically excel file) from dropzone to database? Commented Aug 2, 2016 at 6:06
  • What's in the response tab? Commented Aug 2, 2016 at 6:09
  • @Chris i update the question and added it. Commented Aug 2, 2016 at 6:56

1 Answer 1

1

I have a controller like this

public function upload(Request $request) {
    // validation etc
    // ...

    // I have a table and therefore model to list all excels
    $excelfile = ExcelFile::fromForm($request->file('file'));

    // return whater ...
}

In my ExcelFile Model

protected   $baseDir = 'uploads/excels';

public static function fromForm(UploadedFile $file) {
    $excelfile = new static;

    $name = time() . $file->getClientOriginalName();
    $name = preg_replace('/\s+/', '', $name);
    $excelfile->path = $excelfile->baseDir . '/' . $name;

    $file->move($excelfile->baseDir, $name);

    return $excelfile;
}

You will also need to add UploadedFile in your Model

use symfony\Component\HttpFoundation\File\UploadedFile;

My dropzone is defined like this to ensure correct token handling

<form action="/users/{{ $id }}/media/excelupload" id="drop-zone" class="dz dropzone">
    {{ csrf_field() }}
</form>

<script>

    new Dropzone("#drop-zone", { 
        maxFilesize: 3, // MB
        maxFiles: 10,
        dictDefaultMessage: "Upload Excel.",
        init: function() {
            var known = false;
            this.on("success", function(file, responseText) {
                // do stuff
            });
            this.on('error', function() {
               // aler stuff
            });
            this.on("addedfile", function() {
                if (this.files[10]!=null){
                    this.removeFile(this.files[0]);
                    if (known === false) {
                        alert('Max. 10 Uploads!')
                        known = true;
                    }
                }
            });
        }
    });
</script>

I hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

i follow your example and got this error: Type error: Argument 1 passed to App\ExcelFile::fromForm() must be an instance of App\UploadedFile, instance of Illuminate\Http\UploadedFile
You need to add "use symfony\Component\HttpFoundation\File\UploadedFile;" at the very top ( EDIT: pasted wrong snippet) I'll update the answer in a second

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.