JQuery上传插件uploadify优化
旧版的uploadify是基于flash上传的,但是总有那么些问题,让我们用的不是很舒服。今天主要分享下在项目中用uploadify遇到的一些问题,以及优化处理
下面是官方下载包含的一些文件,当然很多我们在项目中是不需要的

效果图:

下面贴代码:
document.write('<link href="/js/uploadify/uploadify.css" rel="stylesheet" type="text/css" />');
document.write('<script src="/js/uploadify/jquery.uploadify.js" type="text/javascript"></script>');
(function ($) {
$.fn.uploadDOC = function (callback, guid, url, queueID,removeCompleted) {
/// <summary>
/// 基于jQuery上传插件uploadify封装。
/// </summary>
/// <param name="callback" type="function">
/// 上传成功时执行的回调函数。
/// </param>
/// <param name="guid" type="String">
/// 传入产生的随机guid。
/// </param>
/// <param name="url" type="String">
/// 传入地址。
/// </param>
/// <param name="queueID" type="String">
/// 上传队列展示的div或span对应的id。
/// </param>
/// <param name="removeCompleted" type="Boolean">
/// 移除已上传成功的队列。Ture:执行移除;False:保留队列。
/// </param>
$(this).uploadify({
'auto': true,
'buttonImage': '/images/bjdz_btn_xzwj.gif',
'buttonText': '点击上传',
'fileSizeLimit': '5MB',
'fileTypeDesc': '请选择Word文件',
'fileTypeExts': '*.doc;*.docx',
'height': 27,
'method': 'post',
'multi': false,
'queueID': queueID == undefined ? "some" : queueID,
'queueSizeLimit': 1,
'removeCompleted': removeCompleted,
'swf': '/js/uploadify/uploadify.swf',
'uploader': url == undefined ? 'http://imgupload.kuaiyoudai.com/user/uploadcommon.ashx?para=' + guid : url,
'width': 76,
'overrideEvents': ['onUploadSuccess'],
'onUploadSuccess': function onUploadSuccess(file, data, response) {
if (typeof callback === "function") {
if (response) {
callback(data);
}
}
}
});
}
$.fn.upload = function (callback, guid, url, queueID, removeCompleted) {
/// <summary>
/// 基于jQuery上传插件uploadify封装。
/// </summary>
/// <param name="callback" type="function">
/// 上传成功时执行的回调函数。
/// </param>
/// <param name="guid" type="String">
/// 传入产生的随机guid。
/// </param>
/// <param name="url" type="String">
/// 传入地址。
/// </param>
/// <param name="queueID" type="String">
/// 上传队列展示的div或span对应的id。
/// </param>
/// <param name="removeCompleted" type="Boolean">
/// 移除已上传成功的队列。Ture:执行移除;False:保留队列。
/// </param>
$(this).uploadify({
'auto': true,
'buttonImage': '/images/bjdz_btn_xzwj.gif',
'buttonText': '点击上传',
'fileSizeLimit': '5MB',
'fileTypeDesc': '请选择图片文件',
'fileTypeExts': '*.jpg;*.jpeg;*.gif;*.png;',
'height': 27,
'method': 'post',
'multi': false,
'queueID': queueID == undefined ? "some" : queueID,
'queueSizeLimit': 1,
'removeCompleted': removeCompleted,
'swf': '/js/uploadify/uploadify.swf',
'uploader': url == undefined ? 'http://imgupload.kuaiyoudai.com/user/uploadcommon.ashx?para=' + guid : url,
'width': 76,
'overrideEvents': ['onUploadSuccess'],
'onUploadSuccess': function onUploadSuccess(file, data, response) {
if (typeof callback === "function") {
if (response) {
callback(data);
}
}
}
});
}
})($);
upload.js
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="uploadify_Demo.Demo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="/js/jquery-1.8.3.js" type="text/javascript"></script>
<script src="/js/upload.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#btn_Upload").upload(function (data) { alert(data); }, "<%= Guid.NewGuid().ToString() %>");
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" name="" value="上传文件" id="btn_Upload" />
</div>
</form>
</body>
</html>
Demo.html
下面分享下开发中遇到的问题:

官方的直接用会有一些问题
1、多发了一个对于网站根目录的请求

解决方法:
在jquery.uploadify.js文件中找到下面这段代码
this.settings.upload_url = SWFUpload.completeURL(this.settings.upload_url);this.settings.button_image_url = SWFUpload.completeURL(this.settings.button_image_url)
替换为
this.settings.upload_url
=
SWFUpload.completeURL(this.settings.upload_url);this.settings.button_image_url
= this.settings.button_image_url ?
SWFUpload.completeURL(this.settings.button_image_url) :
this.settings.button_image_url
参考自:http://unixlzx.blog.163.com/blog/static/102773752201332505110380/
2、每次带有上传功能的页面初始化都会对swf文件发三次请求

这个问题在网上查了几天,去uploadify社区去看了几天老外的各种提问,没找到解决方法,这个对于上传功能是没有任何影响的,但是开着不怎么爽,就自己调试找方法解决,当然皇天不负有心人,找到了问题的根源,有几种解决方法,我给大家分享一个我这次用的。
对于自己写一个上传插件,不用uploadify也有想过,不过时间上不怎么够,所以就对uploadify进行了简单的处理
效果图:

只有一次对于swf文件的请求了,而且对于网站根目录的请求没有了
原先对与swf文件多发的两次请求的语句分别是
$('#' + swfuploadify.movieName).wrap($wrapper);
和
// Adjust the styles of the movie
$('#' + swfuploadify.movieName).css({
'position' : 'absolute',
'z-index' : 1
});
大家应该找到共同之处了,原因就是调用了 flash 生成的object对象,我的解决方法是避免调用这个对象,所以我在项目中将我的上传按钮 强制必须 要求放到一个div(容器,p标签,span标签都行)中
官方也就是想把Object对象放入到一个div中进行处理,我也就按他们的意思,只不过反其道而位置
不多说,贴代码
if (flashInstalled) {
var swfuploadify = new SWFUpload(swfUploadSettings); ;
// Add the SWFUpload object to the elements data object
$this.data('uploadify', swfuploadify);
$('#' + swfuploadify.movieName).parent().attr('id', settings.id).addClass('uploadify').css({
'height': settings.height + 'px',
'width': settings.width + 'px'
});
// Recreate the reference to wrapper
var $wrapper = $('#' + settings.id);
// Add the data object to the wrapper
$wrapper.data('uploadify', swfuploadify);
// Create the button
var $button = $('<div />', {
'id': settings.id + '-button',
'class': 'uploadify-button ' + settings.buttonClass
});
if (settings.buttonImage) {
$button.css({
'background-image': "url('" + settings.buttonImage + "')",
'text-indent': '-9999px'
});
}
$button.html('<span class="uploadify-button-text">' + settings.buttonText + '</span>')
.css({
'height': settings.height + 'px',
'line-height': settings.height + 'px',
'width': settings.width + 'px',
'margin-top': '-' + settings.height + 'px'
});
// Append the button to the wrapper
$wrapper.append($button);
// Create the file queue
if (!settings.queueID) {
var $queue = $('<div />', {
'id': settings.id + '-queue',
'class': 'uploadify-queue'
});
$wrapper.after($queue);
swfuploadify.settings.queueID = settings.id + '-queue';
swfuploadify.settings.defaultQueue = true;
}
// Create some queue related objects and variables
swfuploadify.queueData = {
files: {}, // The files in the queue
filesSelected: 0, // The number of files selected in the last select operation
filesQueued: 0, // The number of files added to the queue in the last select operation
filesReplaced: 0, // The number of files replaced in the last select operation
filesCancelled: 0, // The number of files that were cancelled instead of replaced
filesErrored: 0, // The number of files that caused error in the last select operation
uploadsSuccessful: 0, // The number of files that were successfully uploaded
uploadsErrored: 0, // The number of files that returned errors during upload
averageSpeed: 0, // The average speed of the uploads in KB
queueLength: 0, // The number of files in the queue
queueSize: 0, // The size in bytes of the entire queue
uploadSize: 0, // The size in bytes of the upload queue
queueBytesUploaded: 0, // The size in bytes that have been uploaded for the current upload queue
uploadQueue: [], // The files currently to be uploaded
errorMsg: '某些文件无法加入到上传队列中:'
};
// Save references to all the objects
//swfuploadify.original = $clone;
//swfuploadify.wrapper = $wrapper;
//swfuploadify.button = $button;
swfuploadify.queue = $queue;
// Call the user-defined init event handler
if (settings.onInit) settings.onInit.call($this, swfuploadify);
} else {
// Call the fallback function
if (settings.onFallback) settings.onFallback.call($this);
}
代码从flash加载成功开始
最后我修改后的uploadify.js文件去除了一些英文提示,加入国人都能看懂的中文提示。
还修改了修正了一处Bug
// Triggered when a file is not added to the queue
onSelectError : function(file, errorCode, errorMsg) {
// Load the swfupload settings
var settings = this.settings; // Run the default event handler
if ($.inArray('onSelectError', settings.overrideEvents) < 0) {
switch(errorCode) {
case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
if (settings.queueSizeLimit > errorMsg) {
this.queueData.errorMsg += '\nThe number of files selected exceeds the remaining upload limit (' + errorMsg + ').';
} else {
this.queueData.errorMsg += '\nThe number of files selected exceeds the queue size limit (' + settings.queueSizeLimit + ').';
}
break;
case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
this.queueData.errorMsg += '\nThe file "' + file.name + '" exceeds the size limit (' + settings.fileSizeLimit + ').';
break;
case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
this.queueData.errorMsg += '\nThe file "' + file.name + '" is empty.';
break;
case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT: //这边bug SWFUpload.QUEUE_ERROR.INVALID_FILETYPE
this.queueData.errorMsg += '\nThe file "' + file.name + '" is not an accepted file type (' + settings.fileTypeDesc + ').';
break;
}
}
if (errorCode != SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
delete this.queueData.files[file.id];
} // Call the user-defined event handler
if (settings.onSelectError) settings.onSelectError.apply(this, arguments);
},
官方的第四个selectError判断重复了,将其修改为case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
下面贴个项目的下载地址,虽然很简单,但是免去大家重写js的痛苦
JQuery上传插件uploadify优化的更多相关文章
- jquery上传插件uploadify 报错http error 302 解决方法之一
前段时间用到jquery上传插件uploadify时,始终出现系统报出 http error 302 的错误. 网上大量搜集信息,基本上都是说session值丢失的问题,根据网友提供的解决方案进行修改 ...
- 【转】JQuery上传插件Uploadify使用详解及错误处理
转自:http://www.jb51.net/article/43498.htm 关于JQuery上传插件Uploadify使用详解网上一大把,基本上内容都一样.我根据网上的步骤配置完成后,会报一些错 ...
- JQuery上传插件Uploadify使用详解
本文转载http://www.cnblogs.com/oec2003/archive/2010/01/06/1640027.html Uploadify是JQuery的一个上传插件,实现的效果非常不错 ...
- (转)JQuery上传插件Uploadify使用详解
原文地址:http://www.cnblogs.com/oec2003/archive/2010/01/06/1640027.html Uploadify是JQuery的一个上传插件,实现的效果非常不 ...
- jQuery上传插件Uploadify使用帮助
Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.它的功能特色总结如下: 支持单文件或多文件上传,可控制并发上传的文件数 在服务器端支持各种语言与之配合使用,诸如PHP, ...
- JQuery上传插件Uploadify使用详解 asp.net版
先来一个实例 Uploadify插件是JQuery的一个文件支持多文件上传的上传插件,ajax异步上传,实现的效果非常不错,带进度显示.不过官方提供的实例时php版本的,本文将详细介绍Uploadif ...
- 文件上传利器JQuery上传插件Uploadify
在做日常项目中,经常在后台需要上传图片等资源文件,之前使用过几次这个组件,感觉非常好用 ,但是每次使用的时候都是需要经过一番查阅,所以还不如记住在这里,以后使用的时候就翻翻. 他的官方网站如下:htt ...
- jQuery上传插件Uploadify 3.2在.NET下的详细例子
项目中要使用Uploadify 3.2来实现图片上传并生成缩略通的功能,特此记下来,以供各位参考! Uploadify下载地址:http://www.uploadify.com/download/ 下 ...
- JQuery上传插件Uploadify详解及其中文按钮解决方案 .
Uploadify有一个参数是 buttonText 这个无论你怎么改都不支持中文,因为插件在js里用了一个转码方法把这个参数的值转过码了,解码的地方在那个swf文件里,看不到代码,所以这条路不行. ...
随机推荐
- [linux] cp: omitting directory `XXX'问题解决
在linux系统中复制文件夹时提示如下: cp: omitting directory `foldera/' 其中foldera是我要复制的文件夹名,出现该警告的原因是因为foldera目录下还存在目 ...
- 按后退键退出Android程序
主要的目的是按后退键的时候,让程序能够退出. 实现起来的思路: 1.捕获后退键被按了这个事件. Java代码 @Override public boolean onKeyDown(int keyCo ...
- Eclipse换常用的快捷键
还是喜欢ctrl+tab键来切换窗口,ctrl+f6实在不好使. 修改方法:在eclipse中Window -> Perferences -> General -> Keys -&g ...
- Python基础教程【读书笔记】 - 2016/7/5
希望通过博客园持续的更新,分享和记录Python基础知识到高级应用的点点滴滴! 第三波:第8章 异常 [总览]学习如何创建和引发自定义的异常,以及处理异常的各种方法. 为了能够处理异常事件,可以再所 ...
- CentOS6.8安装Redis3.2.5
1.下载Redis3.2.5安装包 wget http://download.redis.io/releases/redis-3.2.5.tar.gz 2.解压.编译.安装redi ...
- MySQL存储引擎【InnoDB、MyISAM、Memory】
数据库,MySQL这样存在多存储引擎的数据库软件,清楚常见的存储引擎的区别,使用合适的存储引擎,使得项目跑的更顺畅,有时候对于一个项目,甚至比项目本身都重要.这篇文章,旨在浅谈常见的三种存储引擎的区别 ...
- 打开SDK Manager检查Android SDK下载和更新失败的解决方法
[故障描述] 打开SDK Manager检查Android SDK状况,出现以下情况: Failed to fetch URL https://dl-ssl.google.com/android/r ...
- linux下mongodb定时备份指定的集合
目标:把一台linux机上mongodb的数据定时备份到另一台机上: 过程: 一开始打算使用mongoexport和mongoimport,但是总是会报“\x00”字符串不能识别的问题,后来就改成了m ...
- (四)java程序基本组成
一个基本的java程序一般包括几个部分,分别是程序所在的包名.程序中用到的其他包的路径.程序的类.类中的方法.变量和字面量. package demo; import java.util.Date; ...
- PLSQL_性能优化系列11_Oracle Bulk Collect批处理
2014-10-04 Created By BaoXinjian