当Windows刷新或关闭时重置dropzone

时间:2016-10-28 02:23:15

标签: javascript jquery dropzone.js

我有dropzone文件和按钮保存,在dropzone上如果我上传文件到dropzone将自动移动到我的文件夹,当我按下按钮保存时,该文件名将保存在mysql数据上。现在的问题是,我想在我按下保存按钮之前刷新页面或关闭页面时我的文件获得重置dropzone?这是不可能的? 在这里我的代码!!

关于HTML

<div class="dropzone form-control input-sm" action="<?php echo SERVER_NAME; ?>upload/file" id="dropZone">
                            <div class="fallback" >
                                <input type="file" name="file" id="file"/>
                            </div>
                        </div>

On Jquery

Dropzone.options.dropZone = {
            //options here
            maxFilesize: 1,
            addRemoveLinks: true,
            removedfile: function(file) {
                var name = file.name;        
                $.ajax({
                    type: 'POST',
                    url: host+'upload/unfile',
                    data: "id="+name,
                    dataType: 'html'
                });
                var _ref;
                return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;   

                //console.log();
            }

        }

on contorler

public function file(){
            if (!empty($_FILES)) {
                $tempFile = $_FILES['file']['tmp_name'];
                $fileName = $_FILES['file']['name'];
                $fileType = $_FILES['file']['type'];
                $fileSize = $_FILES['file']['size'];
                $targetPath = './public/uploads/';
                $targetFile = $targetPath . $fileName ;

                //var_dump($_FILES);
                move_uploaded_file($tempFile, $targetFile);
            }
        }

        public function unfile() {
            $fileName = $_POST["id"];
            //var_dump($fileName);
            $targetPath = './public/uploads/';
            $targetFile = $targetPath . $fileName ;
            unlink($targetFile);

        }

注意:我需要在刷新/关闭页面时上传文件但不保存mysql将在我的文件夹中删除。

1 个答案:

答案 0 :(得分:0)

没关系,我发现它,我使用这个代码,并完全删除我的文件:)

$(window).on('beforeunload', function(){
              return 'Are you sure you want to leave? Warning!!! Data is still progress will not be saved';
        });

        $(window).on('unload', function(){
                var file = $('.dropzone .dz-preview').children().find('.dz-filename').find('span').html();                    
                $.ajax({
                    type: 'POST',
                    url: host+'upload/unfile',
                    data: "id="+file,
                    dataType: 'html',
                    async : false
                });
                //alert('Your Out!!');
        });

如果某人遇到像我一样的问题,请不要使用async : false那么重要!!!