旧版的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的痛苦

uploadify_Demo

JQuery上传插件uploadify优化的更多相关文章

  1. jquery上传插件uploadify 报错http error 302 解决方法之一

    前段时间用到jquery上传插件uploadify时,始终出现系统报出 http error 302 的错误. 网上大量搜集信息,基本上都是说session值丢失的问题,根据网友提供的解决方案进行修改 ...

  2. 【转】JQuery上传插件Uploadify使用详解及错误处理

    转自:http://www.jb51.net/article/43498.htm 关于JQuery上传插件Uploadify使用详解网上一大把,基本上内容都一样.我根据网上的步骤配置完成后,会报一些错 ...

  3. JQuery上传插件Uploadify使用详解

    本文转载http://www.cnblogs.com/oec2003/archive/2010/01/06/1640027.html Uploadify是JQuery的一个上传插件,实现的效果非常不错 ...

  4. (转)JQuery上传插件Uploadify使用详解

    原文地址:http://www.cnblogs.com/oec2003/archive/2010/01/06/1640027.html Uploadify是JQuery的一个上传插件,实现的效果非常不 ...

  5. jQuery上传插件Uploadify使用帮助

    Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.它的功能特色总结如下: 支持单文件或多文件上传,可控制并发上传的文件数 在服务器端支持各种语言与之配合使用,诸如PHP, ...

  6. JQuery上传插件Uploadify使用详解 asp.net版

    先来一个实例 Uploadify插件是JQuery的一个文件支持多文件上传的上传插件,ajax异步上传,实现的效果非常不错,带进度显示.不过官方提供的实例时php版本的,本文将详细介绍Uploadif ...

  7. 文件上传利器JQuery上传插件Uploadify

    在做日常项目中,经常在后台需要上传图片等资源文件,之前使用过几次这个组件,感觉非常好用 ,但是每次使用的时候都是需要经过一番查阅,所以还不如记住在这里,以后使用的时候就翻翻. 他的官方网站如下:htt ...

  8. jQuery上传插件Uploadify 3.2在.NET下的详细例子

    项目中要使用Uploadify 3.2来实现图片上传并生成缩略通的功能,特此记下来,以供各位参考! Uploadify下载地址:http://www.uploadify.com/download/ 下 ...

  9. JQuery上传插件Uploadify详解及其中文按钮解决方案 .

    Uploadify有一个参数是 buttonText 这个无论你怎么改都不支持中文,因为插件在js里用了一个转码方法把这个参数的值转过码了,解码的地方在那个swf文件里,看不到代码,所以这条路不行. ...

随机推荐

  1. JS日期加减,日期运算

    一.日期减去天数等于第二个日期 function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 ...

  2. android操作sdcard中的多媒体文件(一)——音乐列表的制作

    android操作sdcard中的多媒体文件(一)——音乐列表的制作 原文地址 最近做了一个android音乐播放器,个人感觉最难的就是“后台播放”以及有关“播放列表”的部分,但是总算是找到了实现的方 ...

  3. 异步I/O编程实例,异步socket

    下载地址: http://files.cnblogs.com/badnewfish/RUYEESocket.rar ———————————————————————————————————————— 举 ...

  4. 三星电视删除USB播放记录

    当使用三星UA40系列电视机播放USB设备中的文件后,会将播放的历史记录存储在电视里.如果想要清除这些播放记录,需要将电视机复位,操作方法如下: 这里提醒您:复位后电视的所有设置都将恢复为出厂默认设置 ...

  5. VB TreeView控件使用详解

    来源:http://www.newxing.com/Tech/Program/VisualBasic/TreeView_587.html 三小时快速掌握TreeView树状控件的使用.能不能掌握控件的 ...

  6. windows mobile仿真器内存调整

    1.打开VS,进入工具,选项. 2.点击设备,在右侧选中要调整的模拟器,点属性. 3.点击仿真器选项. 4.勾选 指定RAM大小. 5.重启仿真管理器.

  7. sql server 2012 如何收缩事务日志

    sql2008不再支持 BACKUP LOG 数据库名 WITH NO_LOG   语句 BACKUP Log zxta with no_log 截断事务日志 sql2008 提示错误如下 BACKU ...

  8. TCP非阻塞通信

    一.SelectableChannel SelectableChannel支持阻塞和非阻塞模式的channel 非阻塞模式下的SelectableChannel,读写不会阻塞 SelectableCh ...

  9. (VS) TFS lost mapping suddenly.

    家里的网络不是很稳定.今天突然发现 TFS 上所有的 mapping都突然没有了. 尝试去remapping,在Source Control Explorer 中右击源文件,然后选择 Advanced ...

  10. Navicat 11使用技巧

    入门 Navicat 是一个可多重连接的数据库管理工具,它可让你连接到 MySQL.Oracle.PostgreSQL.SQLite.SQL Server 和/或 MariaDB 数据库,让管理不同类 ...