使用webuploader插件做导入功能,但是每导入一个文件,控制器就进入两次,不懂原因,求教各位大神,谢谢!
前台代码:
/** * 导入工具类 * Created by huangli on 2017/08/10 */ define(function (require, exports, module) { var defaultOptions = { swf: staticWebUrl + '/lib/webuploader/v0.1.5/Uploader.swf', auto : true, //自动上传 // auto : false, //用户控制上传时机 fileVal : "file", fileNumLimit : 1, duplicate : false, server : webRoot + "import/imStClinic", //按钮 pick : { //按钮ID id : "#importBtn", //按钮文字 innerHTML : "导入", //按钮是否支持多选,默认不支持 multiple : false }, //允许的文件格式 accept : { title : "Excel", extensions : "xlsx,xls", mimeTypes : "application/xls,application/xlsx" } }; var internal = { init : function(options){ var uploadOptions = $.extend(true, {}, defaultOptions, options); var uploader = WebUploader.create(uploadOptions); //添加文件错误 uploader.on( 'error', function(type, file) { if (type == "Q_EXCEED_NUM_LIMIT") { //该错误的file为undefined type = "文件数量超出限制,最多" + uploader.options.fileNumLimit + "个!"; } else if (type == "Q_EXCEED_SIZE_LIMIT") { type = "文件大小超出限制!"; } else if (type == "Q_TYPE_DENIED") { type = "文件类型错误!请选择" + uploader.options.accept[0].extensions; } else if (type == "F_DUPLICATE") { type = "文件重复!"; } else if (type == "Q_FILE_CANNOT_BE_EMPTY") { type = "文件内容不能为空!"; } common.jsmsgError(type); }); // 文件上传失败 uploader.on( 'uploadError', function(file , response) { //common.jsmsgError("导入失败,错误编码:" + response + "!"); common.jsmsgError("导入失败!"); }); //当所有文件上传结束时触发 uploader.on('uploadFinished',function(file, response){ uploader.reset(); }); // 文件上传成功,给item添加成功class, 用样式标记上传成功。 uploader.on( 'uploadSuccess', function( file , response) { //导入不需要写 }); //上传返回值 uploader.on("uploadAccept", function(file, response){ //错误信息在这里提示 if (response.isSuccess) { common.jsmsgSuccess("导入成功"); } else { var errorMsg; if ( response.messages && response.messages.length > 0) { $.each(response.messages, function (index, item) { errorMsg += item["content"] }); } common.jsmsgError(errorMsg); } }); //当有文件添加进来时 /* uploader.on("fileQueued", function(file, response){ common.confirm("是否上传文件", function(){ uploader.upload(); },function(){ uploader.reset(); }); //common.jsmsgSuccess("文件上传成功"); }); */ } }; exports.init = function (options) { return internal.init(options); }; });
后台代码:
@RequestMapping("/imStClinic") @ResponseBody @MethodAction(operateCode = OperateCode.Query, functionName = "导入标准诊项字典") public ServiceResultT<String> imStClinic(@RequestParam("file") MultipartFile photoUpload, HttpServletRequest request, HttpServletResponse response) throws Exception { ServiceResultT<String> result = new ServiceResultT<>(); InputStream input = photoUpload.getInputStream(); String params = request.getParameter("params"); JSONObject obj = JSONObject.parseObject(params); String modelName = obj.getString("modelName"); String resultMsg = ServiceFactory.getImpOrExpService().impCommonInvoke(modelName,input); result.setIsSuccess(true); result.setResult("成功"); return result; }
webuploader插件有一个配置,就是如果你只上传了一张文件给后台,但是配置中设置了文件分段传送,比如说分成了两端,所以会访问两次后台代码,它可以将两次上传的文件保存下来再合并成一个,如果觉得复杂你就改下那个配置作为不分段上传就可以啦。。可以去看下webuploader插件的API
chunked
{Boolean} [可选] [默认值:false]是否要分片处理大文件上传。
chunkSize
{Boolean} [可选] [默认值:5242880]如果要分片,分多大一片? 默认大小为5M