SpringMVC+SwfUpload进行多文件同时上传
发布时间:2020-12-15 01:51:58 所属栏目:大数据 来源:网络整理
导读:由于最近项目需要做一个多文件同时上传的功能,所以好好的看了一下各种上传工具,感觉uploadify和SwfUpload的功能都比较强大,并且使用起来也很方便。是一个flash和js相结合而成的文件上传插件,而且该插件可以用在php、.net、Java等项目开发中。在使用的过
由于最近项目需要做一个多文件同时上传的功能,所以好好的看了一下各种上传工具,感觉uploadify和SwfUpload的功能都比较强大,并且使用起来也很方便。是一个flash和js相结合而成的文件上传插件,而且该插件可以用在php、.net、Java等项目开发中。在使用的过程中需要引入两个js文件,并且进行相关参数的配置,同时也可以定义一系列的事件,如:上传成功事件,上传失败事件,正在上传事件等等。由于我在项目开发是使用SpringMVC进行的,所以下面我根据我自己的项目简述一下SwfUpload如何整合到Web项目中去。 首先说一下Swfupload相对于HTML中file标签上传的优点:
SWfUpload实现文件的上传流程如下: 1、引入相应的js文件 (swfupload.js和swfupload.queue.js必须引入)2、实例化SWFUpload对象,传入一个配置参数对象进行各方面的配置。?3、点击SWFUpload提供的Flash按钮(也可以更改配置信息中的button_image_url参数来配置自己的按钮),弹出文件选取窗口选择要上传的文件;?4、文件选取完成后符合规定的文件会被添加到上传的队列里;?5、调用startUpload方法让队列里文件开始上传;?6、文件上传过程中会触发相应的事件(想要触发什么事件需要自己定义,但是一般网上搜一下就可以了,大部分定义都相同,我也会贴出我的代码),开发者利用这些事件来更新ui、处理错误、发出提示等等;?下面说我的项目中SpringMVC+SwfUpload的配置过程及代码: 引入相应的JS文件后实例化SwfUpload对象(JS代码,一般嵌入到jsp文件中或者单独写一个文件引入到jsp页面内):
window.onload = settings = flash_url : "swfupload/swfupload.swf" flash9_url : "swfupload/swfupload_fp9.swf" upload_url: "http://localhost:8080/ams/upload/fileUpload.do" post_params: {"PHPSESSID" : "aa" file_size_limit : "100 MB" file_types : "*.*" file_post_name : "filedata" file_types_description : "All Files" file_upload_limit : 100 file_queue_limit : 0 progressTarget : "fsUploadProgress" cancelButtonId : "btnCancel"
debug:
button_image_url: "images/TestImageNoText_65x29.png" button_width: "65" button_height: "29" button_placeholder_id: "spanButtonPlaceHolder" button_text: '' button_text_style: ".theFont { font-size: 16; }" button_text_left_padding: 12 button_text_top_padding: 3
swfu = };
编写相关的监听事件(JS代码,即html引入的handler.js文件): (! alert("You need the Flash Player 9.028 or above to use SWFUpload." alert("Something went wrong while loading SWFUpload. If this were a real application we'd clean up and then give you an alternative"
progress = FileProgress(file, progress.setStatus("Pending..." progress.toggleCancel(,
}
(errorCode === alert("You have attempted to queue too many files.n" + (message === 0 ? "You have reached the upload limit." : "You may select " + (message > 1 ? "up to " + message + " files." : "one file."
progress = FileProgress(file, progress.toggleCancel(
progress.setStatus("File is too big." .debug("Error Code: File too big,File name: " + file.name + ",File size: " + file.size + ",Message: " + progress.setStatus("Cannot upload Zero Byte files." .debug("Error Code: Zero byte file,Message: " + progress.setStatus("Invalid File Type." .debug("Error Code: Invalid File Type,Message: " + (file !== progress.setStatus("Unhandled Error" .debug("Error Code: " + errorCode + ",Message: " + }
(numFilesSelected > 0 document.getElementById(.customSettings.cancelButtonId).disabled =
}
progress = FileProgress(file, progress.setStatus("Uploading..." progress.toggleCancel(,
percent = Math.ceil((bytesLoaded / bytesTotal) * 100
progress = FileProgress(file, progress.setStatus("Uploading..." } |