求救!!关于ajaxFileUpload的问题
求救!关于ajaxFileUpload上传文件的问题 本人想做一个上传头像的模拟功能,需要异步显示本地图片信息,我使用ajaxFileUpload插件进行jQuery文件上传,开始用的时候有很多问题,比如没有handlerError函数,我从以前的jQuery版本中复制了一份过来,问题解决了,现在有新的问题了,我从服务端获取的数据总是为空!!!!下面是我的代码和配置:配置部分:<action name="showPicture" class="userAction" method="up">
<result type="json" name="success">
<param name="contentType">
text/html
</param>
</result>
<result type="json" name="error">
<param name="contentType">
text/html
</param>
</result>
</action>
后台处理上传文件的部分:
/**
* 处理文件上传
*/
public void up() {
String savePath = imagePath + this.uploadFileName;
System.out.println("文件路径为:"+upload);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(upload);// 读取文件
fos = new FileOutputStream(savePath);// 写入到我们要保存的路径
IOUtils.copy(fis, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.flush();
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
页面中js调用部分:function uploadImage(){
jQuery.ajaxFileUpload(
{
url:'showPicture.action',
secureuri:false,
fileElementId:'file',
dataType: 'json',
success : function (data, status){
if(typeof(data.error) != 'undefined'){
if(data.error != ''){
alert(data.error);
}else{
alert("success");
}
}
},
error: function (data, status, e){
alert(data.responseText);//此处返回空字符串!!! 头疼!!! }
}
);
}
jsp有关代码:
<div id="newImage">
<center>请选择图片:<s:file id="file" name="upload"></s:file>
<input type="button" value="上传到服务器" onclick="return uploadImage();"/>
</center>
<br><br>
<center><input type="submit" class="btn" value="确认修改"/></center>
</div>
要说明的是,为了去掉返回值中存在的多余的内容,根据网上的建议,我对ajaxFileUpload.js文件中的uploadHttpData函数进行了如下修改:
uploadHttpData : function(r, type) {
var data = !type;
data = type == "xml" || data ? r.responseXML : r.responseText;
data = data.replace("<PRE>", ""); //新加的
data = data.replace("</PRE>", "");//新加的
// If the type is "script", eval it in global context
if (type == "script")
jQuery.globalEval(data);
// Get the JavaScript object, if JSON is used.
if (type == "json")
var data2 = new Array(); //新加的
data2 = data.split("}");//新加的
eval("data = " + data2[0] + "}");//修改后的
// evaluate scripts within html
if (type == "html")
jQuery("<div>").html(data).evalScripts();
return data;
}
我引入了需要的js文件,并且引入的顺序都是对的,在IE中测试时,文件上传成功!但是进入了error中,responseText返回的内容为空!!!!
这是为什么呢?求各位大神解救啊!!!! --------------------编程问答-------------------- 你的后台上传方法都是public void up() void类型的;那你回调函数能得到什么呢? --------------------编程问答-------------------- 既然文件上传成功了,那就只差显示本地图片了。这个图片显示的问题应该不是很难吧,有文件名和路径就可以搞定了。 --------------------编程问答-------------------- uploadImage()就没有返回值,你干嘛在onclick后面加return呢? --------------------编程问答-------------------- 你写的代码有错误。IOUtils.copy(fis, fos);是能将文件上传上去的。你的在你写的up方法里回写东西啊,
HttpServletResponse response = null;这样你在页面就能拿到了,还有在你既然都用返回void类型那你没有必要在配置 <result type="json" name="success">
PrintWriter pw = null;
try {
response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
pw = response.getWriter();
pw.print(str);
} catch (Exception e) {
} finally {
try {
if (pw != null)
pw.close();
} catch (Exception ex) {
}
}
<param name="contentType">
text/html
</param>
</result>
<result type="json" name="error">
<param name="contentType">
text/html
</param>
</result> --------------------编程问答--------------------
我这样改了以后,总是返回到error,但是通过alert(data.responseText)可以看到action中的内容,而且json结构良好但是为什么不返回到success里呢,这样我取不到值啊???? --------------------编程问答--------------------
直接通过action的属性可以传递到请求页面,你这样将数据写入到流中在请求页面又要怎样取值啊?求教 --------------------编程问答--------------------
额。。低级错误,但是改正后数据只能进入error,无法取到值啊... 我取的值都是undefined...???? --------------------编程问答--------------------
return ERROR;
return SUCCESS;
试试
补充:Java , Web 开发