我正在尝试使用 Ajax 和 Spring boot 技术从磁盘(本地或服务器)删除文件。

到目前为止我已经尝试过:

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

Controller :

 @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

请您参考如下方法:

您以错误的格式编写了 @RequestMapping(value = "/removeFile"...)

spring中的路径变量的使用如下

@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"; 
     } 


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!