jquery不使用插件通过ajax方法上传文件
传统的表单也需要定义“multipart/form-data”这样的方式,就是将文件转成二进制流上传到服务器。而jquery的ajax本身貌似本身并没有办法读取本地的文件形成二进制流上传到服务器。只能用插件来实现。
ajax jquery 中的 文件上传问题(jsp+java开发)
试想,如果js就能上传文件,是多么可怕的事情。如果你登录某个网站,这个网站把你的私密文件偷偷都上传了,会是什么结局?
所以浏览器,都要做到安全性考虑,是不会允许js来上传文件的,那么上传文件 必须通过form表单来实现,并且必须通过表单的file文本框来实现,并且必须有用户点击选择文件。
上传文件的安全性处理,是有很深的用意的。
所以你要上传文件,又不想动当前页面的话,建议还是用iframe上传吧。iframe里面套一个form表单。
如果只是在pc网站上用的话,也可以考虑用flash插件、silverlight插件。
jsp中使用jquery的ajaxfileupload插件怎么实现异步上传
ajaxfileupload实现异步上传的完整例子:
JSP页面中引入的script代码:
script
function ajaxFileUpload()
{
$(“#loading”).ajaxStart(function(){
$(this).show();
})//开始上传文件时显示一个图片
.ajaxComplete(function(){
$(this).hide();
});//文件上传完成将图片隐藏起来
$.ajaxFileUpload({
url:’AjaxImageUploadAction.action’,//用于文件上传的服务器端请求地址
secureuri:false,//一般设置为false
fileElementId:’imgfile’,//文件上传空间的id属性 input type=”file” id=”imgfile” name=”file” /
dataType: ‘json’,//返回值类型 一般设置为json
success: function (data, status) //服务器成功响应处理函数
{
alert(data.message);//从服务器返回的json中取出message中的数据,其中message为在struts2中定义的成员变量
if(typeof(data.error) != ‘undefined’)
{
if(data.error != ”)
{
alert(data.error);
}else
{
alert(data.message);
}
}
},
error: function (data, status, e)//服务器响应失败处理函数
{
alert(e);
}
}
)
return false;
}
/script
struts.xml配置文件中的配置方法:
struts
package name=”struts2″ extends=”json-default”
action name=”AjaxImageUploadAction” class=”com.test.action.ImageUploadAction”
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
/package
/struts
上传处理的Action ImageUploadAction.action
package com.test.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings(“serial”)
public class ImageUploadAction extends ActionSupport {
private File imgfile;
private String imgfileFileName;
private String imgfileFileContentType;
private String message = “你已成功上传文件”;
public File getImgfile() {
return imgfile;
}
public void setImgfile(File imgfile) {
this.imgfile = imgfile;
}
public String getImgfileFileName() {
return imgfileFileName;
}
public void setImgfileFileName(String imgfileFileName) {
this.imgfileFileName = imgfileFileName;
}
public String getImgfileFileContentType() {
return imgfileFileContentType;
}
public void setImgfileFileContentType(String imgfileFileContentType) {
this.imgfileFileContentType = imgfileFileContentType;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@SuppressWarnings(“deprecation”)
public String execute() throws Exception {
String path = ServletActionContext.getRequest().getRealPath(“/upload/mri_img_upload”);
String[] imgTypes = new String[] { “gif”, “jpg”, “jpeg”, “png”,”bmp” };
try {
File f = this.getImgfile();
String fileExt = this.getImgfileFileName().substring(this.getImgfileFileName().lastIndexOf(“.”) + 1).toLowerCase();
/*
if(this.getImgfileFileName().endsWith(“.exe”)){
message=”上传的文件格式不允许!!!”;
return ERROR;
}*/
/**
* 检测上传文件的扩展名是否合法
* */
if (!Arrays.String asList(imgTypes).contains(fileExt)) {
message=”只能上传 gif,jpg,jpeg,png,bmp等格式的文件!”;
return ERROR;
}
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + “/”+ this.getImgfileFileName());
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
message = “文件上传失败了!!!!”;
}
return SUCCESS;
}
}