dtef9322 2016-08-02 05:00
浏览 98
已采纳

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

Controller

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

Request Preview [enter image description hereRequest Response

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

Did i miss something or what?

  • 写回答

1条回答 默认 最新

  • drhozgt6007 2016-08-02 06:09
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。