Posted on 2008-09-06 12:53
H2O 閱讀(1421)
評論(0) 編輯 收藏 所屬分類:
ajax
首先要說的就是 ajax 是無法實現上傳文件的,可以想一下ajax與后臺通信都是通過傳遞字符串,怎么能傳遞文件呢?其實出于安全考慮js是不能操作文件的,所以就不要再說用ajax來實現文件的上傳了,這是不可能的。
而本文實現的文件上傳也是無頁面刷新的,可以說是一種"類似AJAX"方法。
開始之前先說兩句無關的,其實在ajax出現之前,web應用也可以是無刷新的,那時大多通過IFrame來做到這一點。當然Ajax出現之后,人們一窩 蜂地投奔Ajax 的陣營了,iFrame 就乏人問津了。但是用iFrame來實現無刷新上傳文件確實一個很好的選擇。ps:Ajax技術基本上可以說是由google公司帶起來的,但少 Gmail中上傳文件用的還是 IFrame,所以說使用IFrame來上傳文件是最好的選擇。
我在這里這里用的技術是jsp,其實asp,php等也是一樣可以這么實現的
一共兩個文件就可實現:index.html 和 upload.jsp.
index.html
<html>
<body>
<form action="upload.jsp" id="form1" name="form1" encType="multipart/form-data" method="post" target="hidden_frame" >
<input type="file" id="file" name="file" style="width:450">
<INPUT type="submit" value="上傳文件"><span id="msg"></span>
<br>
<font color="red">支持JPG,JPEG,GIF,BMP,SWF,RMVB,RM,AVI文件的上傳</font>
<iframe name='hidden_frame' id="hidden_frame" ></iframe>
</form>
</body>
</html>

<script type="text/javascript">
function callback(msg)


{
document.getElementById("file").outerHTML = document.getElementById("file").outerHTML;
document.getElementById("msg").innerHTML = "<font color=red>"+msg+"</font>";
}
</script>
index.html 中主要要做的就是寫一個 form 和 iframe ,并把 form 的 target 設為 iframe 的名字,注意要把 iframe 設為不可見,其他的都是正常的文件上傳的寫法,這樣刷新的頁面就是這個隱藏的 Iframe ,而在 index.html 中是不會有頁面刷新的,js的 callback 方法是回調方法。用于清空文件上傳框和顯示后臺信息,注意清空文件上傳框的方法,和普通方法有點不一樣。
<%@ page language="java" contentType="text/html; charset=gb2312" %>
<%@ page import="com.jspsmart.upload.SmartUpload"%>
<%
//新建一個SmartUpload對象

SmartUpload su = new SmartUpload();
//上傳初始化

su.initialize(pageContext);
// 設定上傳限制

//1.限制每個上傳文件的最大長度。

su.setMaxFileSize(10000000);
//2.限制總上傳數據的長度。

su.setTotalMaxFileSize(20000000);
//3.設定允許上傳的文件(通過擴展名限制),僅允許doc,txt文件。

su.setAllowedFilesList("doc,txt,jpg,rar,mid,waw,mp3,gif");
boolean sign = true;
//4.設定禁止上傳的文件(通過擴展名限制),禁止上傳帶有exe,bat,jsp,htm,html擴展名的文件和沒有擴展名的文件。


try
{
su.setDeniedFilesList("exe,bat,jsp,htm,html");
//上傳文件

su.upload();
//將上傳文件保存到指定目錄

su.save("c:\\");

} catch (Exception e)
{
e.printStackTrace();
sign = false;
}
if(sign==true)

{
out.println("<script>parent.callback('upload file success')</script>");
}else

{
out.println("<script>parent.callback('upload file error')</script>");
}
%>
upload.jsp 中只要注意最后輸出的格式就可以了。其實原理就是輸出一段js代碼到 iframe 中,然后在iframe中來控制它的父頁面。
OK,至此一個無刷新的頁面上傳組件就做好了,不要忘了在 WEB-INF/lib 下加上必須的 jspSmartUpload.jar 包。
需要說明的是使用Iframe來上傳,狀態欄還是會有刷新的,因為iframe 中的頁面刷新了嘛,但是外部頁面,就是你所看到的頁面是沒有刷新的,所以也可以說是類似Ajax上傳。
下載
String filePath = "/文件夾1/文件夾2/JAVA教程.doc";
String fileName = "JAVA教程";
String serverPath = request.getRealPath("");


response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-disposition","attachment;filename=" + new String(fileName.getBytes(),"iso8859-1"));

java.io.FileInputStream fileInputStream = null;
PrintWriter out = null;

try {
}{
fileInputStream = new java.io.FileInputStream(serverPath + filePath);
out = response.getWriter();

int i;
while ((i=fileInputStream.read()) != -1) {
out.write(i);
}

} catch(FileNotFoundException fe){
}{
fe.printStackTrace();
System.out.println(errMsg= "file is not exist");
}

catch (Exception e) {
}{
e.printStackTrace();
System.out.println(errMsg= "download failed");

} finally{
}{
if(fileInputStream != null)
fileInputStream.close();
if(out != null)
out.close();
}
