1
我正在嘗試製作一個需要上載文件的上傳頁面。我在這裏使用Spring框架我的查詢是在Upload按鈕上我調用了一個JavaScript方法,它應該使用jQuery AJAX將我的文件發送給控制器。有沒有什麼辦法通過JavaScript來傳遞?如何使用javascript發送文件
以下是我正在嘗試的代碼。
<body>
<div style="text-align: center; margin-top: 60px;">
<form enctype="multipart/form-data">
Select file:
<input type="file" name="dataFile" id="fileAttachment"/><br/><br/>
<div style="text-align: center; margin-top: 100px;">
<input style="cursor: pointer;" onmouseover="" onclick="uploadAttachment()" class="dialogbox" type="submit" value="Upload Report" />
</div>
</form>
</div>
</body>
JS:
<script language="Javascript">
function uploadAttachment(){
var Name = jQuery('#Name option:selected').text();
jQuery.post('upload',{Name:Name}
function(data){
if(data==1)
alert("success");
else
alert("failed");
});
}
</script>
以下controller.java頁是代碼編寫
@RequestMapping(value = "upload", method=RequestMethod.POST)
public @ResponseBody String upload(HttpServletRequest request, HttpServletResponse response,
@RequestParam("Name") String Name){
System.out.println(Name);
}
你到底想要什麼。你想在同一頁面顯示上傳的文件細節嗎?就像'gmail'顯示附件詳細信息一樣? –
我只是想通過Jquery.post() – b22
發送選擇上傳到控制器的文件您可以發佈您的控制器代碼? –