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

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

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择