百度 flash html5自切换 多文件异步上传控件webuploader基本用法
双核浏览器下在chrome内核中使用uploadify总有302问题,也不知道如何修复,之所以喜欢360浏览器是因为帮客户控制渲染内核:
若页面需默认用极速核,增加标签:<meta name="renderer" content="webkit"> 若页面需默认用ie兼容内核,增加标签:<meta name="renderer" content="ie-comp"> 若页面需默认用ie标准内核,增加标签:<meta name="renderer" content="ie-stand">
要解决302问题也很简单,就是html5的文件上传,正好最近在ueditor里看到百度的webuploader,会自动选择flash html5,就是一个成熟的解决方案了。
先看前端,我们将最常用的操作封装为插件,asp.net中和MVC中最好使用相对于应用程序的绝对路径,自行定义全局applicationPath :var applicationPath = "@(Href("~")=="/"?"":Href("~"))";
前端代码:
(function ($, applicationPath) { var applicationPath = applicationPath === "" ? "" : applicationPath || "../.."; function initWebUpload(item, options) { var defaults = { hiddenInputId: "uploadifyHiddenInputId", // input hidden id onAllComplete: function (event) { }, // 当所有file都上传后执行的回调函数 onComplete: function (event) { },// 每上传一个file的回调函数 innerOptions: {}, fileNumLimit: undefined, fileSizeLimit: undefined, fileSingleSizeLimit: undefined }; var opts = $.extend({}, defaults, options); var target = $(item); var pickerid = guidGenerator().replace(/-/g, ""); 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‘), state = ‘pending‘, uploader; var jsonData = { fileList: [] }; var webuploaderoptions = $.extend({ // swf文件路径 swf: applicationPath + ‘/Scripts/lib/webuploader/Uploader.swf‘, // 文件接收服务端。 server: applicationPath + ‘/MvcPages/WebUploader/Process‘, // 选择文件的按钮。可选。 // 内部根据当前运行是创建,可能是input元素,也可能是flash. pick: ‘#‘ + pickerid, // 不压缩image, 默认如果是jpeg,文件上传前会压缩一把再上传! resize: false, fileNumLimit: opts.fileNumLimit, fileSizeLimit: opts.fileSizeLimit, fileSingleSizeLimit: opts.fileSingleSizeLimit }, opts.innerOptions); var uploader = WebUploader.create(webuploaderoptions); uploader.on(‘fileQueued‘, function (file) { var $list = $("#thelist"); $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‘, function (file, percentage) { var $li = $(‘#‘ + 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‘, function (file, response) { $(‘#‘ + 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‘, function (file) { $(‘#‘ + file.id).find(‘div.state‘).text(‘上传出错‘); }); uploader.on(‘uploadComplete‘, function (file) { $(‘#‘ + file.id).find(‘.progress‘).fadeOut(); var fp = $("#" + opts.hiddenInputId); fp.val(JSON.stringify(jsonData)); opts.onAllComplete(jsonData.fileList); }); uploader.on(‘fileQueued‘, function (file) { uploader.upload(); }); uploader.on(‘filesQueued‘, function (file) { uploader.upload(); }); uploader.on(‘all‘, function (type) { if (type === ‘startUpload‘) { state = ‘uploading‘; } else if (type === ‘stopUpload‘) { state = ‘paused‘; } else if (type === ‘uploadFinished‘) { state = ‘done‘; } if (state === ‘uploading‘) { $btn.text(‘暂停上传‘); } else { $btn.text(‘开始上传‘); } }); $btn.on(‘click‘, function () { if (state === ‘uploading‘) { uploader.stop(); } else { uploader.upload(); } }); //删除 $list.on("click", ".del", function () { var $ele = $(this); var id = $ele.parent().attr("id"); var deletefile = {}; $.each(jsonData.fileList, function (index, item) { if (item && item.queueId === id) { deletefile = jsonData.fileList.splice(index, 1)[0]; $("#" + opts.hiddenInputId).val(JSON.stringify(jsonData)); $.post(/*自己发送处理删除指令*/, function (returndata) { $ele.parent().remove(); }); return; } }); }); } $.fn.powerWebUpload = function (options) { var ele = this; $.lazyload(applicationPath + "/Scripts/lib/webuploader/webuploader.css", function () { }, ‘css‘); $.lazyLoad(applicationPath + "/Scripts/lib/webuploader/webuploader.min.js", function () { initWebUpload(ele, options); }); } })(jQuery, applicationPath);
MVC版后端文件接收,即便你是asp.net 引入mvc做ajax也是可以的:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; namespace FormDesigner { public class WebUploaderController : BaseController { public ActionResult Process(string id, string name, string type, string lastModifiedDate, int size) { string filePathName = string.Empty; string urlPath = "../../App_Upload/Document"; string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "App_Upload\\Document"); if (Request.Files.Count == 0) { return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失败" }, id = "id" }); } try { filePathName = //自己在这里处理文件保存 } catch (Exception) { return Json(new { jsonrpc = 2.0, error = new { code = 103, message = "保存失败" }, id = "id" }); } return Json(new { jsonrpc = "2.0", id = "id", filePath = urlPath + "/" + filePathName }); } } }
页面后台就自己处理hiddenfiled的内容了。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。