博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个Jquery上传文件插件
阅读量:4993 次
发布时间:2019-06-12

本文共 8451 字,大约阅读时间需要 28 分钟。

 

 

(function ($) {    var checkAction;    $.fn.jUploader = function (opts) {        var opts = $.extend({}, $.fn.jUploader.defaults, opts);        if ($('#jUploader_Frame').length == 0) {            var frame = ''            $('body').append(frame);        }        var Uploader = function (uploader, opts) {            var html = [];            var htmlTextArea = [];            var id = uploader.id;            var $uploader = $(uploader);            html.push('
'); html.push('
'); html.push('
'); html.push('
'); html.push('
'); htmlTextArea.push('
'); $('body').append(html.join('')).append(htmlTextArea.join('')); var $container = $('#' + id + '_Container'); var $form = $container.find('form'); var $submit = $form.find('input:button'); var $file = $form.find('input:file'); var $textArea = $('#' + id + '_Area'); var $text = $textArea.find('input'); var $buttonArea = $('
' + opts.buttonText + '
'); var $processArea = $('
Uploading...
'); var top, left, height, offset, width, outHeight, outWidth, zIndex var offset = $uploader.offset(); if (opts.repositionOnResize == true) { $(window).bind('resize.' + id, function () { Reposition(id); }); } $uploader.keydown(function (event) { event.preventDefault(); }); $file.change(function () { $submit.click(); }); $submit.click(function () { $buttonArea.hide(); $processArea.show(); ResetStatus(); clearInterval(checkAction); checkAction = setInterval('CheckStatus("' + id + '");', 200); opts.uploadStart.call(null); $form.submit(); }); top = offset.top; left = offset.left; width = $uploader.width() height = $uploader.height(); outHeight = $uploader.outerHeight(); outWidth = $uploader.outerWidth(); zIndex = $.ui.dialog.maxZ + 1 $container.css({ opacity: 0.0, position: 'absolute', top: top + 'px', left: left + 'px', width: outWidth + 82 + 'px', 'z-index': zIndex }); $file.css({ float: 'right', height: outHeight + 'px' }); $textArea.css({ position: 'absolute', top: top + 'px', left: left + 'px', 'z-index': zIndex }); $text.css({ width: width + 'px', height: height + 'px' }).attr('disabled', 'disabled').val($uploader.val()); $buttonArea.css({ position: 'absolute', height: outHeight + 'px', top: top + 'px', left: left + outWidth + 'px', 'z-index': zIndex - 1 }); $processArea.css({ position: 'absolute', height: outHeight + 'px', top: top + 'px', left: left + outWidth + 'px', 'z-index': zIndex - 1 }); $('body').append($buttonArea).append($processArea); $processArea.hide(); return $uploader; } var Reposition = function (uploaderId) { var $uploader = $('#' + uploaderId); var left, offset, outWidth; var offset = $uploader.offset(); left = offset.left; outWidth = $uploader.outerWidth(); $('#' + uploaderId + '_Container').css({ left: left + 'px' }); $('#' + uploaderId + '_Area').css({ left: left + 'px' }); $('#' + uploaderId + '_Button').css({ left: left + outWidth + 'px' }); $('#' + uploaderId + '_Process').css({ left: left + outWidth + 'px' }); } window.CheckStatus = function (uploaderId) { var frameContent = document.getElementById('jUploader_Frame').contentWindow; var $frameBody = $(frameContent.document.getElementsByTagName('body')[0]); var $lblStatus = $frameBody.find('#lblStatus'); var $lblMessage = $frameBody.find('#lblMessage'); if ($lblStatus.length > 0) { clearInterval(checkAction); $('#' + uploaderId + '_Process').hide(); $('#' + uploaderId + '_Button').show(); var status = $lblStatus.text(); var message = $lblMessage.text(); if (status == "Success") { var value = $('#' + uploaderId + '_File').val(); $('#' + uploaderId).val(value); $('#' + uploaderId + '_Text').val(value); } else { $('#' + uploaderId + '_File').replaceWith('
'); var $file = $('#' + uploaderId + '_File'); $file.css({ float: 'right', height: $('#' + uploaderId).outerHeight() + 'px' }); $file.change(function () { $('#' + uploaderId + '_Submit').click(); }); } opts.uploadComplete.call(null, { Status: status, Message: message }); } } var ResetStatus = function () { var frameContent = document.getElementById('jUploader_Frame').contentWindow; var $frameBody = $(frameContent.document.getElementsByTagName('body')[0]); $frameBody.empty(); } return this.each(function () { return new Uploader(this, opts); }); } $.fn.jUploader.defaults = { action: '../UserControls/FileUploader.ashx', uploadFolder: 'User', buttonText: 'Browse', repositionOnResize: false, uploadStart: function () { }, uploadComplete: function (args) { } }; $.fn.jUploaderDestroy = function () { this.each(function () { var id = this.id; $('#' + id + '_Container').remove(); $('#' + id + '_Area').remove(); $('#' + id + '_Button').remove(); $('#' + id + '_Process').remove(); $(window).unbind('resize.' + id); }); }})(jQuery);

 

.jUploaderButtton{
width: 80px;background-color: #ab12ff;text-align: center;}.jUploaderProcess{
width: 90px;text-indent: 30px;background-image: url('images/ui-anim_basic_16x16.gif');background-repeat: no-repeat;background-position: 15% 50%;}

用法如下

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUploader.aspx.cs" Inherits="eComm.Maint.Web.CommomUserControls.FileUploader1" %>        

 

public partial class FileUploader1 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            string strStatus = "Failed";            string strMessage = "Upload failed.";            try            {                HttpRequest request =Request;string imgFolder = string.Empty;for (int i = 0; i < request.Files.Count; i++)                {                    HttpPostedFile postedFile = request.Files[i];                    if (Path.GetExtension(postedFile.FileName).ToUpper() != ".JPG")                    {                        strMessage = "Format error,Only support JPG file.";                        return;                    }                    string filePath = Path.Combine(imgFolder, Path.GetFileName(postedFile.FileName));                    postedFile.SaveAs(filePath);                }                strStatus = "Success";                strMessage = "Upload Success.";            }            catch (Exception ex)            {                strMessage = ex.Message;            }            finally            {                lblStatus.InnerText = strStatus;                lblMessage.InnerText = strMessage;                lblStatus.Visible = true;                lblMessage.Visible = true;            }        }    }

 

 

 

 

转载于:https://www.cnblogs.com/edward44444/archive/2012/04/24/2467565.html

你可能感兴趣的文章
Android开发 - Retrofit 2 使用自签名的HTTPS证书进行API请求
查看>>
对测试人员或开发人员来说相互沟通有多重要?
查看>>
解释器、编译器以及他们之间的差别。
查看>>
MongoDB的快速手动安装
查看>>
JS制作简单的日历控件【JS Date对象操作实例演示】
查看>>
模板—树上倍增LCA
查看>>
高二小假期集训—D5
查看>>
EasyUI easyui-combobox 重复发送请求
查看>>
memcached-repcached
查看>>
[转]CentOS 5.3通过yum升级php到最新版本的方法
查看>>
UVA 11235 - Frequent values RMQ的应用
查看>>
大数据日志采集系统
查看>>
java 堆调优
查看>>
linux 安装JDK
查看>>
JAVA调用CMD命令
查看>>
weblogic的安装
查看>>
SSM框架中,controller的action返回参数给vue.js
查看>>
Mysql 基础3
查看>>
smartctl工具应用(转载整理)
查看>>
控件数据绑定总结
查看>>