ajax文件下载插件|jsp中使用jquery的ajaxfileupload插件怎么实现异步上传

A. ajaxupload.js无法获取上传文件

ajaxupload.js是一个开源的实现Ajax文件上传的jQuery插件,常用于向服务器上传文件。如果遇到该插件无法获取上传文件的问题,猜郑可能是以下几个方面的原因:1、插件引用错误:首先需要确保插件已正确引用,可以检查HTML代码或控制台中是否出现引用错误或没有引用相关JS文件的情况。2、表单设置错误:使用ajaxupload.js时,需要将表单对象传皮兆衫递给插件以表示文件上传的上下文。如果表单对象设置有误,插件可能无法获取上传文件。可以确保表单中存在file类型的input,并将其作为参数传递给插件。3、上传文件格式不正确:如果上传文件的格式不受支持,则插件可能无法正确获取上传文件。可以确保上传的文件格式与插件支持的格式相同。4、安全问题限制:有些情况下,安全设置可能会禁止通过javaScript读取文件内容,防止网站被黑客攻击等。可以检查安全设置以确定是否存在限制。5、程序服务端问题:如果前面几个问题都排除了,还是无法获取上传文件,则可能是由于程序服务端的设置或代码实现问题,需要进一步检查或调试燃腔。

B. 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.actionpackage 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; }}


赞 (0)