2016-10-28 40 views
0

我有dropzone文件和按鈕保存,在dropzone,如果我uploadfile進入dropzone是會自動移動到我的文件夾,當我按下按鈕保存,該文件名將保存在MySQL數據。現在的問題是,我希望我的文件得到重置dropzone,當我刷新頁面或關閉頁面之前,我壓按鈕保存?那是不可能的? 在這裏我的代碼!重置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> 

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

     } 

上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將被刪除在我的文件夾上。

回答

0

沒關係,我發現它,我用這個代碼,並prefectly刪除我的文件:

$(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是如此重要!