ajax 上传文件
1
2
3
4
5
6
<input id="file" name="myfile" onclick="clearProgressInfo()" type="file">
<span id="progressInfo" style="display:none;">
<progress id="progressBar" value="0" max="100"></progress>
<span id="percentage"></span>
</span><br>
<input onclick="UpladFile()" value="上传" type="button">
js代码?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function UpladFile() {
var fileObj = document.getElementById("file").files; // js 获取文件对象
var FileController = "/file/saveFile.do"; // 接收上传文件的后台地址
// FormData 对象
var form = new FormData();
form.append("author", "hooyes"); // 可以增加表单数据
form.append("file", fileObj); // 文件对象
// XMLHttpRequest 对象
var xhr = new XMLHttpRequest();
xhr.open("post", FileController, true);
xhr.onload = function() {
// alert("上传完成!");
};
document.getElementById('progressInfo').style.display = "";
xhr.upload.addEventListener("progress", progressFunction, false);
xhr.send(form);
}
function clearProgressInfo()
{
document.getElementById('progressInfo').style.display='none';
document.getElementById("progressBar").value = 0;
document.getElementById("percentage").innerHTML = "";
}
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) + "%";
}
}
java后台处理:?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.util.Streams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.okcoin.util.Logs;
@Controller
@RequestMapping(value="/file/*.do")
public class FileController extends MultiActionController{
public String saveFile(HttpServletRequest request,HttpServletResponse response) throws IOException
{
MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
MultipartHttpServletRequest multipartRequest = null;
try {
multipartRequest = resolver.resolveMultipart(request);
} catch (Exception e) {
Logs.geterrorLogger().error("AdminAccountController submitVerifyFile 非常的请求");
}
//接收文件
MultipartFile documentFile = multipartRequest.getFile("file");
//获取文件名
String documentFileName=documentFile.getOriginalFilename();
System.out.println("FileName:" + documentFileName);
BufferedInputStream in = new BufferedInputStream(documentFile.getInputStream());
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\Users\\" + documentFileName));
Streams.copy(in, out, true);
System.out.println("copy finished.");
return "";
}
}
然而楼主想表达什么呢?http://www.shinybbs.com/?fromuid=626565
http://www.ygbc.top/?fromuid=2438
初级上传,很有学习参考的价值 关于ajax的提示,不是提示框,是直接显示在页面上的文字,楼主有木有…… Enrique丶 发表于 2015-7-23 15:06
关于ajax的提示,不是提示框,是直接显示在页面上的文字,楼主有木有……
你指的是不要用alert ,改innerHTML吗{:301_1008:},我是新手 打错勿喷 阿伦找茬 发表于 2015-7-24 13:06
你指的是不要用alert ,改innerHTML吗,我是新手 打错勿喷
恩,我是用innerHTML做的,想问问有木有其他方法…… Enrique丶 发表于 2015-7-24 14:18
恩,我是用innerHTML做的,想问问有木有其他方法……
doucument.write() 或者是doucument.writeln()还有就是jquery的html()方法 这个其实就是innerHTML 火钳刘明……楼煮上传成功了咩
页:
[1]