ajax上传图片收不到后台传的路径
jsp:<s:form id="uploadimage" action="upload" theme="simple"enctype="multipart/form-data">
<label> 上传你的头像吧: </label> <img id="touxiang"
src="image/touxiang.jpg" alt="" />
<s:file accept="image/gif, image/jpeg" id="imagepath" name="file"></s:file>
<s:submit value="提交"></s:submit>
</s:form>
js:$("#imagepath").change(function(){
$("#uploadimage").ajaxSubmit({
type: 'get',
url: "upload",
success: function(data){
alert(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert("error");
}
});return false;
});
Action:
@SuppressWarnings("deprecation")
public void uploadImage() throws IOException{
String filepath = ServletActionContext.getRequest().getRealPath("/uploadimage");
InputStream in=new FileInputStream(file.get(0));
File imageFile=new File(filepath,getFileFileName().get(0));
OutputStream out=new FileOutputStream(imageFile);
byte[] buffer=new byte[1024];
while(in.read(buffer)!=-1){
out.write(buffer,0,buffer.length);
}
in.close();
out.close();
PrintWriter pw= response.getWriter();
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-store"); //HTTP1.1
response.setHeader("Pragma", "no-cache"); //HTTP1.0
response.setDateHeader("Expires", 0); //prevents catching at proxy server
pw.write(filepath);
pw.flush();
pw.close();
}
图片能传上 但是一直alert("error"); --------------------编程问答-------------------- Ajax 现在能支持 multipart/form-data 的 MIME 么?好像不行吧!
你一般看到所谓的 Ajax 文件上传要就是使用 Flash 插件,或者是 iframe 形式模拟的。 --------------------编程问答-------------------- 而且你还用的是 GET 请求,要知道 multipart/form-data 必须使用 POST 请求才行。 --------------------编程问答-------------------- --------------------编程问答--------------------
诶呀妈呀,昨天就想用 iframe做,但是还没动手,又看了看 from plugin 插件,打算马上用的,但是今天出去了, 没想到,居然有人问了。
纠结用 iframe 还是 flash了。。。
目前也是学习阶段。 --------------------编程问答-------------------- 本来写的是post方式 但是网上找 是说ajax请求跨域 解决方法是在两个文件里都添加一段 js: document.domain 这边不太懂 --------------------编程问答-------------------- 用jquery.uploadify吗? --------------------编程问答--------------------
++1
补充:Java , Java EE