2017-08-31 31 views
0

我想從使用Ajax和Spring引導技術的磁盤(本地或服務器)中刪除文件。使用AJAX和Spring引導從磁盤刪除文件

到目前爲止,我已經試過這樣:

阿賈克斯/ jQuery的:

  $(".ct-color-specs").on("click",".color-spec-file-delete",function() { 
        var deletedFileName = $(this).parents(".ct-attached-color-spec-files").find("a").text(); 
        $.ajax({ 

         url : "/Application/removeFile/"+deletedFileName", 
         type: 'DELETE', 
         success: function (res) { 
          console.log(data); 
         } 
        }); 
       }); 

控制器:

@RequestMapping(value = "/removeFile",produces="text/html", method = RequestMethod.DELETE) 
    public String removeFileHandler(@PathVariable("deletedFileName") String filepath, Model model) { 
     String removeFileCheck = "false"; 
     try{ 

      System.out.println("Delete filepath from AJX"); 
      File file = new File(filepath); 

      if(file.delete()){ 
       System.out.println(file.getName() + " is deleted!"); 
       removeFileCheck="true"; 
      }else{ 
       System.out.println("Delete operation is failed."); 
      } 

     }catch(Exception e){ 

      e.printStackTrace(); 

     } 
     model.addAttribute("checkList", removeFileCheck); 
     return "p/view"; 
    } 

錯誤:

"Not Found" message : "No message available" path : "/Application/removeFile/File.pdf" status : 404

+2

您使用的類型後,但鑑於RequestMethod.DELETE – Barath

+0

我指的是這個答案https://stackoverflow.com/questions/10766195/spring-3-jquery-ajax-delete –

+0

檢查ajax調用類型,請求將其更改爲鍵入:'DELETE', – Barath

回答

0

我將在這裏書寫的答案被使用,因爲我已經解決了下面的代碼。

控制器:

@RequestMapping(value = "/removeFile/{deletedFileName}", method = RequestMethod.GET) 
public String removeFileHandler(@PathVariable("deletedFileName") String filepath, Model model) { 
     ..... 
    } 

AJAX/jQuery的:

  $(".ct-color-specs").on("click",".color-spec-file-delete",function() { 
        var deletedFileName = $(this).parents(".ct-attached-color-spec-files").find("a").text().split('/').pop().split('\\').pop();; 
        alert("deletedFileName--->" + deletedFileName); 
        $.ajax({ 
         url : "/Application/removeFile/"+deletedFileName, 
         type: 'GET', 
         success: function (res) { 
          console.log(data); 
         } 
        }); 
       }); 
0

您已經格式錯誤寫入@RequestMapping(值=「/ removeFile」 ...)

春季Path變量就像下面

@RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET) 
    public String getLogin(@PathVariable("userId") String userId, 
     @PathVariable("roleId") String roleId){ 
     System.out.println("User Id : " + userId); 
     System.out.println("Role Id : " + roleId); 
     return "hello"; 
    } 
相關問題