我想從使用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
您使用的類型後,但鑑於RequestMethod.DELETE – Barath
我指的是這個答案https://stackoverflow.com/questions/10766195/spring-3-jquery-ajax-delete –
檢查ajax調用類型,請求將其更改爲鍵入:'DELETE', – Barath