Dojo是一项蛮新的前台技术,作为一种javascript工具集,学起来不太难,就是网上有用的资料不是很多,有些东西要靠自己慢慢捉摸~~~
dojo的异步传输功能还是很方便的,有三种,具体就不说了,网上可以查到.但如果要用到上传文件的话,只有用dojo.io.iframe方法来传输了.
先帖前台,节选了功能核心部分代码:
<script type="text/javascript" src="dojo-lib/dojo/dojo.js" djConfig="parseOnLoad: true"></script> <script type="text/javascript"> dojo.require("dojo.parser"); dojo.require("dojo.io.iframe"); function handler(response, ioArgs) { dojo.byId("background").src = response; if(!(response instanceof Error)) { alert("文件上传成功!"); } else { alert("文件上传失败!"); } } function uploadImage() { dojo.io.iframe.send({ form: "upload_form", url: "uploadFileSave.action", method: "post", handleAs: "text", handle: handler }); }
Action类代码:
package com.action.dojoAction;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadFileSave extends ActionSupport {
private static final long serialVersionUID = 1L;
private static final int BUFFER_SIZE = 16 * 1024 ;
private File myFile;
private String fileName;
private InputStream textStream;
public InputStream getTextStream() {
return textStream;
}
public void setTextStream(InputStream textStream) {
this.textStream = textStream;
}
public void setMyFileFileName(String fileName) {
this.fileName = fileName;
}
public String getFileName()
{
return fileName;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
private static void copy(File src, File dst) {
try
{
InputStream in = null ;
OutputStream out = null ;
try
{
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0 )
{
out.write(buffer);
}
}
finally
{
if (null != in)
{
in.close();
}
if (null != out)
{
out.close();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static String getExtention(String fileName)
{
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
private static String getHtmlFilePath(String filePath)
{
StringBuffer str = new StringBuffer();
str.append("<html><head></head><body>");
str.append("<textarea>");
str.append(filePath);
str.append("</textarea>");
str.append("</body></html>");
return str.toString();
}
public String execute() throws Exception {
// TODO Auto-generated method stub
String imageFileName;
imageFileName = new Date().getTime() + getExtention(fileName);
String filePath = "UploadImages\\" + imageFileName;
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("\\UploadImages") + "\\" + imageFileName);
copy(myFile, imageFile);
try
{
textStream = new ByteArrayInputStream(getHtmlFilePath(filePath).getBytes("UTF-8"));
}
catch(IOException e)
{
e.printStackTrace();
}
return SUCCESS;
}
public void validate()
{
System.out.println("AjaxAction-UploadFileSave: " + "validate()");
}
}
struts配置代码:
<action name="uploadFileSave" class="com.action.dojoAction.UploadFileSave"> <result name="success" type="stream"> <param name="contentType">text/plain</param> <param name="inputName">textStream</param> <param name="bufferSize">1024</param></result> <result name="input" type="chain">imageUploadInit</result> </action>
按我的理解解释一下:首先dojo.io.iframe.send函数中的handleAs类型比较常用的是html,json,text这几种,这里面有个地方很莫名其妙,除了html格式的其余几种格式都要写成"<html><head></head><body><textarea>data</textarea></body></html>"这样的形式,data是你要传输的数据,这个当时让我超级无语.还有一点就是struts的配置问题,要配置成stream类型返回,照着上面配就可以了.最后要说明的一点就是dojo用这种方法的本质还是用了iframe框架来实现的,ajax是不能异步传输文件的,所以dojo也就不可能用ajax传值的那种正统的异步传输方式实现异步~~~