【发布时间】:2017-08-31 05:10:01
【问题描述】:
我正在尝试使用 Ajax 和 Spring 引导技术从磁盘(本地或服务器)中删除文件。
到目前为止,我已经尝试过:
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);
}
});
});
控制器:
@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";
}
错误:
“未找到”消息:“没有可用消息”路径: “/Application/removeFile/File.pdf”状态:404
【问题讨论】:
-
你使用了 POST 类型但给定了 RequestMethod.DELETE
-
检查ajax调用类型,要求你改成type:'DELETE',
-
需要一个好的解决方案,这个答案对我没有帮助
标签: java jquery ajax spring spring-boot