springMVC+ajax實現(xiàn)文件上傳且?guī)нM度條實例
更新時間:2017年01月18日 09:40:18 作者:肖哥哥
本篇文章主要介紹了springMVC+ajax實現(xiàn)文件上傳且?guī)нM度條實例,具有一定的參考價值,有興趣的可以了解一下。
前端代碼:
<form id= "uploadForm">
<p >指定文件名: <input type="text" name="filename" value= ""/></p >
<p >上傳文件: <input type="file" name="file"/></ p>
<input type="button" value="上傳" onclick="doUpload()" />
</form>
function doUpload() {
var formData = new FormData($( "#uploadForm" )[0]);
$.ajax({
url: 'http://localhost:8080/xiaochangwei/file/upload' ,
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (returndata) {
alert(returndata);
},
error: function (returndata) {
alert(returndata);
}
});
}
后端:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(HttpServletRequest request, @RequestParam("file") MultipartFile file, ModelMap model) {
System.out.println("開始");
String path = request.getSession().getServletContext().getRealPath("upload");
String fileName = file.getOriginalFilename();
// String fileName = new Date().getTime()+".jpg";
System.out.println(path);
File targetFile = new File(path, fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
// 保存
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("fileUrl", request.getContextPath() + "/upload/" + fileName);
return "result";
}
如果前端有很多實體類數(shù)據(jù)同文件一同提交
可以修改后端方法為:
復(fù)制代碼 代碼如下:
upload(HttpServletRequest request, @RequestParam("file") MultipartFile file, ModelMap model,User user)
利用下面的代碼更可實現(xiàn)帶有進度條的文件上傳
<script type="text/javascript">
function UpladFile() {
var fileObj = document.getElementById("file").files[0]; // js 獲取文件對象
var FileController = "http://localhost:8080/xiaochangwei/file/upload"; // 接收上傳文件的后臺地址
// FormData 對象
var form = new FormData($( "#uploadForm" )[0]);
// XMLHttpRequest 對象
var xhr = new XMLHttpRequest();
xhr.open("post", FileController, true);
xhr.onload = function () {
// alert("上傳完成!");
};
xhr.upload.addEventListener("progress", progressFunction, false);
xhr.send(form);
}
function progressFunction(evt) {
var progressBar = document.getElementById("progressBar");
var percentageDiv = document.getElementById("percentage");
if (evt.lengthComputable) {
progressBar.max = evt.total;
progressBar.value = evt.loaded;
percentageDiv.innerHTML = Math.round(evt.loaded / evt.total * 100) + "%";
if(evt.loaded==evt.total){
alert("上傳完成100%");
}
}
}
</script>
<progress id="progressBar" value="0" max="100"></progress>
<form id= "uploadForm">
<input type="file" id="file" name="myfile" />
<input type="button" onclick="UpladFile()" value="上傳" />
</form>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaCV調(diào)用百度AI實現(xiàn)人臉檢測方法詳解
在檢測人臉數(shù)量、位置、性別、口罩等場景時,可以考慮使用百度開放平臺提供的web接口,一個web請求就能完成檢測得到結(jié)果。本文就為大家介紹JavaCV如何調(diào)用百度AI實現(xiàn)最簡單的人臉檢測,需要的可以參考一下2022-01-01
使用SpringBoot整合Activiti6工作流的操作方法
這篇文章主要介紹了使用SpringBoot整合Activiti6工作流,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
Intellij IDEA 與maven 版本不符 Unable to import maven project See
這篇文章主要介紹了Intellij IDEA 與maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound,本文通過圖文給大家分享解決方案,需要的朋友可以參考下2020-08-08
java文件操作報錯:java.io.FileNotFoundException(拒絕訪問)問題
在進行編程時,經(jīng)常會遇到因疏忽小細(xì)節(jié)而導(dǎo)致的錯誤,如忘記在路徑后添加文件名,本文通過一個具體的修改前后對比示例,解釋了錯誤原因,并給出了解決方案,這類經(jīng)驗分享對編程學(xué)習(xí)者具有參考價值2024-10-10
IntelliJ IDEA中properties文件顯示亂碼問題的解決辦法
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中properties文件顯示亂碼問題的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10

