jquery uploadify的JS用法以及JAVA类的用法

$(function(){
	//$('#uploadify').uploadifySettings('folder','JS');  //动态修改参数
	
	$("#uploadify").uploadify({ //初始化函数
	    width: 20,
	    uploader: '<%=basePath%>resources/lib/uploadify/uploadify.swf',
	    //flash文件位置,注意路径
	    // 'swf' : '<%=basePath%>resources/lib/uploadify/uploadify.swf',
	    script: '<%=basePath%>IntentionController.do?method=upload',
	    //后台处理的请求(sevlet)
	    buttonImg: '<%=basePath%>resources/lib/uploadify/cancel.png',
	    //上传按钮的背景图片
	    auto: true,
	    //选定文件后是否自动上传,默认false
	    cancelImg: '<%=basePath%>resources/lib/uploadify/cancel.png',
	    //取消按钮图片
	    //'folder' : '<%=basePath%>resources/lib/uploadify/pic',//您想将文件保存到的路径,将auto设置为true里才有用,不然跳到类里去处理,那就那里说了算
	    queueID: 'fileQueue',
	    //与下面的上传文件列表id对应
	    queueSizeLimit: 8,
	    //上传文件的数量
	    scriptData: {
	        'userID': ''
	    },
	    //向后台传的数据
	    fileDesc: '',
	    //上传文件类型说明
	    fileExt: 'jpg,png,gif,jpeg',
	    //控制可上传文件的扩展名,启用本项时需同时声明fileDesc
	    method: 'post',
	    //如果向后台传输数据,必须是get
	    //  sizeLimit:10000,//文件上传的大小限制,单位是字节
	    multi: true,
	    simUploadLimit: 8,
	    //同时上传文件的数量,设置了这个参数后,
	    //那么你会因设置multi:true和queueSizeLimit:8而可以多选8个文件,
	    //但如果一旦你将simUploadLimit也设置了,那么只会上传这个参数指定的文件个数,其它就上传不了
	    buttonText: 'BROWSE',
	    //浏览按钮上的文字
	    onComplete: function(event, queueID, fileObj, serverData, data) { //当上传完成后的回调函数,ajax方式哦~~
	        //alert("上传成功");
	        addImg(serverData);
	        // alert(serverData);
	        //addImg(serverData);
	        /* $('#image').attr("src",serverData);//serverData是sevlet中out.print图片的路径
	                              $('#image').show();*/
	    },
	    onSelect: function(e, queueId, fileObj) {
	        /*alert("唯一标识:" + queueId + "\r\n" +
		                  "文件名:" + fileObj.name + "\r\n" +
		                  "文件大小:" + fileObj.size + "\r\n" +
		                  "创建时间:" + fileObj.creationDate + "\r\n" +
		                  "最后修改时间:" + fileObj.modificationDate + "\r\n" +
		                  "文件类型:" + fileObj.type
		            );*/

	    },
	    onError: function(event, queueID, fileObj) { alert("文件:" + fileObj.name + " 上传失败");
	    }
	});
});


 

function addImg(imgUrl){
	var path=[];
	path=imgUrl.split("#");
   var list=$("#imgList");
   var img=$("<img>");
   img.attr("src",path[0]);
   img.attr("id",path[1]);
   img.attr("height","150px");
   img.attr("width","150px");
   img.click(function(){
           alert("设为首页募捐显示照片吗?");
	   });
   img.css("cursor","pointer");
   list.append(img);
 //  proDownImage(path[0],img);
}


 

JAVA文件是这样的:

public String upload(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String basePath = request.getScheme() + "://"
		+ request.getServerName() + ":" + request.getServerPort()
		+ request.getContextPath() + "/";
		
		String returnPath=basePath+"intentionPicture/";
		String savePath =request.getSession().getServletContext().getRealPath("/")+"intentionPicture\\";
		
			File f1 = new File(savePath);
			//System.out.println(savePath);
			if (!f1.exists()) {
				f1.mkdirs();
			}
			DiskFileItemFactory fac = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(fac);
			upload.setHeaderEncoding("utf-8");
			List fileList = null;
			try {
				fileList = upload.parseRequest(request);
			} catch (FileUploadException ex) {
			}
			Iterator<FileItem> it = fileList.iterator();
			String name = "";
			String extName = "";
			while (it.hasNext()) {
				FileItem item = it.next();
				if (!item.isFormField()) {
					name = item.getName();
					long size = item.getSize();
					String type = item.getContentType();
					//System.out.println(size + " " + type);
					if (name == null || name.trim().equals("")) {
						continue;
					}
					// 扩展名格式:
					if (name.lastIndexOf(".") >= 0) {
						extName = name.substring(name.lastIndexOf("."));
					}
					File file = null;
					do {
						// 生成文件名:
						name = UUID.randomUUID().toString();
						file = new File(savePath + name + extName);
						//System.out.println(savePath + name + extName);
					} while (file.exists());
					File saveFile = new File(savePath + name + extName);
					try {
						item.write(saveFile);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}

		return returnPath+name + extName+"#"+savePath+name+extName;
	}


 

JSP文件是这样的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>My JSP 'aaa.jsp' starting page</title>
		<link href="../../resources/lib/ligerUI/skins/Aqua/css/ligerui-all.css"
			rel="stylesheet" type="text/css" />
		 <link
			href="../../resources/ldcss/ldform.css"
			rel="stylesheet" type="text/css" /> 
		<link href="../../resources/ldcss/global.css" rel="stylesheet" type="text/css" />
		
		
		<link href="<%=basePath%>resources/lib/uploadify/uploadify.css" rel="stylesheet" type="text/css" />
		
		
		<script src="<%=basePath%>resources/ldjs/global.js" type="text/javascript"></script>
		<script src="<%=basePath%>resources/lib/jquery/jquery-1.7.1.min.js" type="text/javascript"></script>
			
			
		<script type="text/javascript" src="<%=basePath%>resources/lib/uploadify/swfobject.js"></script>
        <script type="text/javascript" src="<%=basePath%>resources/lib/uploadify/jquery.uploadify.v2.1.0.min.js"></script>
        
        
	</head>	
<body>
	<div id="fileQueue"></div>

		<input type="file" name="uploadify" id="uploadify" />

		<p>

			<a href="javascript:jQuery('#uploadify').uploadifyUpload()">开始上传</a> 

			<a href="javascript:jQuery('#uploadify').uploadifyClearQueue()">取消所有上传</a>

		</p>
		
	<div id="imgList"></div>
		
</body>

</html>


 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Jquery Uploadify 是一个基于 Jquery 的文件上传插件,它使用 Flash 技术来实现文件上传功能。它简单易用,可以方便地进行文件上传,并提供了多个自定义选项,如文件大小限制、文件型限制等。 H5 上传指的是使用 HTML5 中的 File API 来实现文件上传功能。与传统的 Flash、Java Applet 等方式相比,H5 上传更加安全、快速,并且不需要额外安装插件。 使用 Jquery Uploadify 进行 H5 上传,可以通过以下步骤进行操作: 1. 下载并引入 Jquery Uploadify 插件文件,并确保引入了 jQuery 库。 2. 在 HTML 文件中创建一个显示上传文件区域的容器,如: <div id="file-upload-container"> <input type="file" id="file-upload" name="file-upload" multiple> </div> 3. 在 JavaScript 文件中,使用 Jquery 的选择器选取文件上传区域,然后调用 Uploadify 插件的方法进行初始化,如: $("#file-upload").uploadify({ swf: 'uploadify.swf', // 指定 Flash 文件路径 uploader: 'uploadify.php', // 指定上传文件的服务器端处理文件路径 // 其他自定义选项... }); 4. 在服务器端编写处理文件上传的脚本(uploadify.php)。可以使用 PHP、Java、Python 等服务器端语言进行处理,并根据上传结果返回相应的提示信息。 通过以上步骤,我们就可以使用 Jquery Uploadify 插件进行 H5 文件上传了。需要注意的是,由于 H5 上传只能在现代浏览器中完全支持,所以在使用时应该检查浏览器兼容性,确保用户能够正常上传文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值