百度 flash html5自切换 多文件异步上传控件webuploader基本用法
发布时间:2020-12-15 07:04:32 所属栏目:百科 来源:网络整理
导读:?? 双核浏览器下在chrome内核中使用uploadify总有302问题,也不知道如何修复,之所以喜欢360浏览器是因为帮客户控制渲染内核: 若页面需默认用极速核,增加标签: meta name ="renderer" content ="webkit" 若页面需默认用ie兼容内核,增加标签: ="ie-comp"
??
双核浏览器下在chrome内核中使用uploadify总有302问题,也不知道如何修复,之所以喜欢360浏览器是因为帮客户控制渲染内核: 若页面需默认用极速核,增加标签:<meta name="renderer" content="webkit"> 若页面需默认用ie兼容内核,增加标签:="ie-comp"> 若页面需默认用ie标准内核,增加标签:="ie-stand"> ?要解决302问题也很简单,就是html5的文件上传,正好最近在ueditor里看到百度的webuploader,会自动选择flash html5,就是一个成熟的解决方案了。 ? 先看前端,我们将最常用的操作封装为插件,asp.net中和MVC中最好使用相对于应用程序的绝对路径,自行定义全局applicationPath :var applicationPath = "@(Href("~")=="/"?"":Href("~"))";? 前端插件代码: var applicationPath = applicationPath === "" ? "" : applicationPath || "../.."; (function ($,applicationPath) { function initWebUpload(item,options) { var defaults = { hiddenInputId: "uploadifyHiddenInputId",// input hidden id onAllComplete: function (event) { },0); line-height:1.5!important"> 当所有file都上传后执行的回调函数 onComplete: 每上传一个file的回调函数 innerOptions: {},fileNumLimit: undefined,fileSizeLimit: undefined,fileSingleSizeLimit: undefined }; var opts = $.extend({},defaults,options); var target = $(item);容器 var pickerid = ""; if (typeof guidGenerator != 'undefined')给一个唯一ID pickerid = guidGenerator(); else pickerid = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); var uploaderStrdiv = '<div class="webuploader">' + '<div id="thelist" class="uploader-list"></div>' + '<div class="btns">' + '<div id="' + pickerid + '">选择文件</div>' + '<a id="ctlBtn" class="btn btn-default">开始上传</a>' + '</div>' + '</div>'; target.append(uploaderStrdiv); var $list = target.find('#thelist'),$btn = target.find('#ctlBtn'),0); line-height:1.5!important">这个留着,以便随时切换是否要手动上传 state = 'pending',uploader; var jsonData = { fileList: [] }; var webuploaderoptions = $.extend({ swf文件路径 swf: applicationPath + '/Scripts/lib/webuploader/Uploader.swf', 文件接收服务端。 server: applicationPath + '/MvcPages/WebUploader/Process',0); line-height:1.5!important"> 选择文件的按钮。可选。 内部根据当前运行是创建,可能是input元素,也可能是flash. pick: '#' + pickerid,0); line-height:1.5!important"> 不压缩image,默认如果是jpeg,文件上传前会压缩一把再上传! resize: false,fileNumLimit: opts.fileNumLimit,fileSizeLimit: opts.fileSizeLimit,fileSingleSizeLimit: opts.fileSingleSizeLimit },opts.innerOptions); var uploader = WebUploader.create(webuploaderoptions); uploader.on('fileQueued',255); line-height:1.5!important">function (file) {队列事件 $list.append('<div id="' + file.id + '" class="item">' + '<div class="info">' + file.name + '</div>' + '<div class="state">等待上传...</div>' + '<div class="del"></div>' + '</div>'); }); uploader.on('uploadProgress',255); line-height:1.5!important">function (file,percentage) {进度条事件 var $li = target.find('#' + file.id),$percent = $li.find('.progress .bar'); 避免重复创建 if (!$percent.length) { $percent = $('<span class="progress">' + '<span class="percentage"><span class="text"></span>' + '<span class="bar" role="progressbar" style="width: 0%">' + '</span></span>' + '</span>').appendTo($li).find('.bar'); } $li.find('div.state').text('上传中'); $li.find(".text").text(percentage * 100 + '%'); $percent.css('width',percentage * 100 + '%'); }); uploader.on('uploadSuccess',response) {上传成功事件 target.find('#' + file.id).find('div.state').text('已上传'); var fileEvent = { queueId: file.id,name: file.name,size: file.size,type: file.type,filePath: response.filePath }; jsonData.fileList.push(fileEvent) opts.onComplete(fileEvent); }); uploader.on('uploadError',255); line-height:1.5!important">function (file) { target.find('#' + file.id).find('div.state').text('上传出错'); }); uploader.on('uploadComplete',0); line-height:1.5!important">全部完成事件 target.find('#' + file.id).find('.progress').fadeOut(); var fp = $("#" + opts.hiddenInputId); fp.val(JSON.stringify(jsonData)); opts.onAllComplete(jsonData.fileList); }); uploader.on('fileQueued',255); line-height:1.5!important">function (file) { uploader.upload(); }); uploader.on('filesQueued',255); line-height:1.5!important">function (file) { uploader.upload(); }); uploader.on('all',255); line-height:1.5!important">function (type) { if (type === 'startUpload') { state = 'uploading'; } else if (type === 'stopUpload') { state = 'paused'; } if (type === 'uploadFinished') { state = 'done'; } if (state === 'uploading') { $btn.text('暂停上传'); } else { $btn.text('开始上传'); } }); $btn.on('click',255); line-height:1.5!important">function () { if (state === 'uploading') { uploader.stop(); } else { uploader.upload(); } }); 删除 $list.on("click",".del",255); line-height:1.5!important">var $ele = $(this); var id = $ele.parent().attr("id"); var deletefile = {}; $.each(jsonData.fileList,255); line-height:1.5!important">function (index,item) { if (item && item.queueId === id) { deletefile = jsonData.fileList.splice(index,1)[0]; $("#" + opts.hiddenInputId).val(JSON.stringify(jsonData)); $.post(opts.ashx,{ 'type': 'delete','filepathname': deletefile.filePath },255); line-height:1.5!important">function (returndata) { $ele.parent().remove(); }); return; } }); }); } $.fn.powerWebUpload = function (options) { var ele = this; typeof PowerJs != 'undefined') { $.lazyLoad(applicationPath + "/Scripts/lib/webuploader/webuploader.css",255); line-height:1.5!important">function () { },'css'); $.lazyLoad(applicationPath + "/Scripts/lib/webuploader/webuploader.min.js",255); line-height:1.5!important">function () { initWebUpload(ele,options); }); } else { initWebUpload(ele,options); } } })(jQuery,applicationPath); ? ? 页面引入上述js后使用: ?$("#uploader").powerWebUpload({ hiddenInputId:?"uploadhidden"?});? html端需要一个容器和hidden div id="uploadify" class="shortuploader"></div> asp:HiddenField ID="hfFilePath" ClientIDMode="Static" runat="server" /> ? MVC版后端文件接收,即便你是asp.net 引入mvc做ajax也是可以的: ? using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; public class WebUploaderController : BaseController { public ActionResult Process(string id,255); line-height:1.5!important">string name,255); line-height:1.5!important">string type,255); line-height:1.5!important">string lastModifiedDate,255); line-height:1.5!important">int size) { string filePathName = string.Empty; string urlPath = "../../App_Upload/Document"; string localPath = Path.Combine(HttpRuntime.AppDomainAppPath,App_UploadDocument"); if (Request.Files.Count == 0) { return Json(new { jsonrpc = 2.0,error = new { code = 102,message = 保存失败" },id = id" }); } try { filePathName = 自己在这里处理文件保存 Request.Files[0] } catch (Exception) { 103,0); line-height:1.5!important">" }); } new { jsonrpc = 2.0",id = /" + filePathName }); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |