laravel + dropzone文件未上传 [英] laravel + dropzone file not uploading

查看:57
本文介绍了laravel + dropzone文件未上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么即使文件上传成功,我上传的文件也没有反映在请求上?

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
    }
});

控制器

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

请求预览 [ 请求回复

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


我错过了什么还是什么?


Did i miss something or what?

推荐答案

我有一个像这样的控制器

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

在我的ExcelFile模型中

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

您还需要在模型中添加UploadedFile

You will also need to add UploadedFile in your Model

use symfony\Component\HttpFoundation\File\UploadedFile;

我的dropzone定义如下,以确保正确的令牌处理

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>

我希望这对您有帮助

这篇关于laravel + dropzone文件未上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆