Struts2使用FlashFileUpload.swf实现批量文件上传
今天晚上头有些痛,写篇文章,全当休息下吧。
讲两点:一是如何在Struts2中使用FlashFileUpload这个flash的上传插件;二是在使用flash上传插件时如何解决Session问题。
先说第一点,如何在Struts2中使用FlashFileUpload这个flash的上传插件。
以前做.net时,用过一个FlashFileUpload.swf批量文件上传工具,很帅很简单。
网址:http://www.codeproject.com/KB/aspnet/FlashUpload.aspx
因为这个用Flex写的客户端插件公开源码,而且实现的的相当完美,在asp.net中用起来,就是一两行代码的事,所以非常喜欢。
最近想在Struts2中实现批量上传(使用html的file标签时,一次只能选一个文件,我是想一下可以选多个文件),结果找了半天,大都是swf+ajax的,用起来太复杂了,代码也很繁多,实在没耐心了。
就想起FlashFileUpload来了,结果发现不知道怎么用,官网上只给出了C#的例子,上网找了半天也没找到在Struts2中用的例子,甚至jsp的例子也没找到,最后自己研究出来了,在这里公开一下,希望对大家有所帮助。
大家从官网上下载了压缩包解压后,会找到Upload.cs,从代码中可以看到HttpContext对象可以直接把文件对象分离出来,想对上传的临时文件进行转存时,也很方便:
for(int j = 0; j < context.Request.Files.Count; j++) { // get the current file HttpPostedFile uploadFile = context.Request.Files[j]; // if there was a file uploded if (uploadFile.ContentLength > 0) { // save the file to the upload directory //use this if testing from a classic style upload, ie. // <form action="Upload.axd" method="post" enctype="multipart/form-data"> // <input type="file" name="fileUpload" /> // <input type="submit" value="Upload" /> //</form> // this is because flash sends just the filename, where the above //will send the file path, ie. c:\My Pictures\test1.jpg //you can use Test.thm to test this page. //string filename = uploadFile.FileName.Substring(uploadFile.FileName.LastIndexOf("\")); //uploadFile.SaveAs(string.Format("{0}{1}{2}", tempFile, "Upload\", filename)); // use this if using flash to upload uploadFile.SaveAs(Path.Combine(uploadPath, uploadFile.FileName));/** 文件对象 */private List<File> Filedata;/** 文件名 */private List<String> FiledataFileName;/** 文件内容类型 */private List<String> FiledataContentType;/** * @return the filedata */public List<File> getFiledata() {return Filedata;}/** * @param filedata the filedata to set */public void setFiledata(List<File> filedata) {Filedata = filedata;}/** * @return the filedataFileName */public List<String> getFiledataFileName() {return FiledataFileName;}/** * @param filedataFileName the filedataFileName to set */public void setFiledataFileName(List<String> filedataFileName) {FiledataFileName = filedataFileName;}/** * @return the filedataContentType */public List<String> getFiledataContentType() {return FiledataContentType;}/** * @param filedataContentType the filedataContentType to set */public void setFiledataContentType(List<String> filedataContentType) {FiledataContentType = filedataContentType;}
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="100%" height="100%" id="file" align="middle"> <param name="allowScriptAccess" value="sameDomain" /> <param name="movie" value="/flash/FlashFileUpload.swf?ver=2009041702" /> <param name="quality" value="high" /> <param name="wmode" value="transparent"> <param name="FlashVars" value='fileTypeDescription=JPG格式图片&fileTypes=*.jpg;*.jpeg&fileSizeLimit=3000000&totalUploadSize=10240000&uploadPage=/performMultiuploadForDemo;jsessionid=${session.id}?username=${username}'> <embed src="/flash/FlashFileUpload.swf?ver=2009041702" FlashVars='fileTypeDescription=JPG格式图片&fileTypes=*.jpg;*.jpeg&fileSizeLimit=3000000&totalUploadSize=10240000&uploadPage=/performMultiuploadForDemo;jsessionid=${session.id}?username=${username}' quality="high" wmode="transparent" width="100%" height="100%" name="file" align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /> </object>>