ASP.NET使用SWFUpload上传大文件教学(源码) (转)

前段时间从网上转摘了一篇并于在ASP.NET中利用SWFUpload上传的文章,可能原文介绍也不是很详细,很多朋友发信说看不明白或者不会使用,正好在目前作的一个电子政务项目中用到了该方法,把源码贴出如下

代码很简单,有需要的朋友可以自行更改

SWFUPLOAD源码如下:

 


//引入File Api
import flash.net.FileReference;

//变量
var mFile:FileReference = new FileReference();
var mTotalBytes:Number = 0;
var sConver=0;
var sRecname=0;
var mUploadedBytes:Number = 0;
var mFileName:String = "";
var mFileListener = new Object();
var mIsUploading = false;
_root.btnUpload.enabled 
= false;
_root.btnBrowse.addEventListener(
"click",BrowseFile);
_root.btnUpload.addEventListener(
"click",UploadFile);
_root.Conver.addEventListener(
"click",funConver);
_root.recname.addEventListener(
"click",funRecname);
function funRecname()
{
    
if(recname.selected)
    {
        sRecname
=1;
    }
    
else
    {
        sRecname
=0;
    }
}
function funConver()
{
    
if(Conver.selected)
    {    
        sConver
=1;
        getURL(
"javascript:Conver(1);alert('您选择了在线转换视频,这将需要一定时间\\n请不要关闭此窗体及作任何无关动作,耐心等待')");
        
//ShowMessage("您选择了在线转换视频,这将需要一定时间\\n请不要关闭此窗体及作任何无关动作,耐心等待");

    }
    
else
    {
        sConver
=0;
        getURL(
"javascript:Conver(0);");
    }
}
function BrowseFile()
{

    
if(FileExtension != undefined)
    {
        
var allTypes:Array = new Array();

        
var myTypes:Object = new Object();
        myTypes.description 
= "请选择要上传的文件("+FileExtension+")";
        myTypes.extension 
= FileExtension;
        allTypes.push(myTypes);

        mFile.browse(allTypes);
    }
    
else
    {
        mFile.browse();
    }
}

function UploadFile()
{
;
    
if(_root.mIsUploading)
    {
        mFile.cancel();
        _root.btnUpload.label 
= "上传";
        _root.mIsUploading 
= false;
        _root.btnBrowse.enabled 
= true;
    }
    
else
    {
        _root.btnUpload.label 
= "取消";
        _root.mIsUploading 
= true;
        _root.btnBrowse.enabled 
= false;
        mFile.upload(BuildUploadPage());
        
    }
}

//文件上传监视
mFileListener.onSelect = function (file:FileReference):Void {
    
if(file.size > 0)
    {    
var Usize:Number=512
        
if(filesize!=undefined)
        {
            Usize
=filesize;
        }
        
if(file.size>Usize*1024)
        {
            ShowMessage(
"上传大小超过限制("+Usize+"KB)");
        }
        _root.btnUpload.enabled 
= true;
        _root.mFileName 
= file.name;
        _root.mTotalBytes 
= file.size;        
        _root.txtFile.text 
= "["+_root.GetSizeType(_root.mTotalBytes)+"]"+_root.mFileName;
    }
    
else
    {
        _root.btnUpload.enabled 
= false;
    }
}


//取消
mFileListener.onCancel = function (fileRef:FileReference):Void {
    _root.btnUpload.label 
= "上传";
    _root.mIsUploading 
= false;
    
//_root.btnBrowse.enabled = true;
}

mFileListener.onOpen 
= function (fileRef:FileReference):Void {    
    _root.txtFile.text 
= "["+_root.GetSizeType(0)+"/"+_root.GetSizeType(_root.mTotalBytes)+"]"+_root.mFileName;
}

mFileListener.onProgress 
= function (fileRef:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
    _root.txtFile.text 
= "["+_root.GetSizeType(bytesLoaded)+"/"+_root.GetSizeType(bytesTotal)+"]"+_root.mFileName;
    gotoAndStop(
int((bytesLoaded/bytesTotal)*100));
}

mFileListener.onComplete 
= function (fileRef:FileReference):Void {

    _root.btnUpload.label 
= "上传";
    _root.mIsUploading 
= false;
    _root.btnBrowse.enabled 
= true;
    _root.btnUpload.enabled
=false;
    
//_root.txtFile.text="请选择要上传的文件";
    FinishedUpload(fileRef.name);
}

mFileListener.onHTTPError 
= function(fileRef:FileReference):Void {
    mFile.cancel();
    _root.btnUpload.label 
= "上传";
    _root.mIsUploading 
= false;
    _root.btnBrowse.enabled 
= true;
    _root.mcProgressBar.visible 
= false;
    ShowMessage(
"上传[" + fileRef.name+"]时,出现网络问题.");
}

mFileListener.onIOError 
= function(fileRef:FileReference):Void {
    ShowMessage(
"无法打开文件:" + fileRef.name);
}

mFileListener.onSecurityError 
= function(fileRef:FileReference, errorString:String):Void {
    ShowMessage(
"没有上传权限");
}

mFile.addListener(mFileListener);
//弹出信息窗口
function ShowMessage(msg:String)
{
    getURL(
"javascript:alert(\""+msg+"\");")
}

function FinishedUpload(fileName)
{
    
if(CompletedFunction != undefined)
    {
        getURL(
"javascript:"+ CompletedFunction+"('"+fileName+"');");
    }

}

function BuildUploadPage()
{
    
var sUploadPage:String = "SWFFileUpload.axd";
    
if(UploadPage != undefined)
    {
        sUploadPage 
= UploadPage;
    }
    
    
if(Args != undefined)
    {
        
var myArgs:Array = Args.split(";");
        
var strArgs:String = "?a=a&conver="+sConver+"&recname="+sRecname;
        
for (var i = 0; i<myArgs.length; i++
        {
            strArgs 
+="&"+ myArgs[i];
        }
        sUploadPage 
+=strArgs;
    }
    
return sUploadPage;
}

//计算大小
function GetSizeType(size:Number)
{
    
if(size < 1024)
        
return int(size*100)/100 + "bytes";
    if(size < 1048576)
        
return int((size / 1024)*100)/100 + "KB";
    if(size < 1073741824)
       
return int((size / 1048576)*100)/100 + "MB";
    return int((size / 1073741824)*100)/100 + "GB";
}

stop();

 

页面引用源码如下:

 


<script type="text/javascript">
                    document.write(
'<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\"');
                    document.write(
'width=\"550\" height=\"45\" id=\"Object1\" align=\"middle\">')
                    document.write(
'<param name=\"allowScriptAccess\" value=\"sameDomain\" />');
                    document.write(
'<param name=\"movie\" value=\"SWFFileUpload.swf\" />');
                    document.write(
'<param name=\"quality\" value=\"high\" />');
                    document.write(
'<param name=\"wmode\" value=\"transparent\">');
                    document.write(
'<param name=\"FlashVars\" value=\"UploadPage=FLVUpload.aspx&Args=type=mov;path=;ParentPath=&CompletedFunction=OnCompleted&FileExtension=*.mpeg;*.avi;*.mp4;*.3gp;*.rm;*.rmvb;*.mov;*.wmv;*.flv;*.asf&filesize=204800\">');
                    document.write(
'<embed src=\"SWFFileUpload.swf\" name=\"fileUpload2\" align=\"middle\"');
                    document.write(
'allowscriptaccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />');
                    document.write(
'</object>');
                
</script>

 

FLVUpload.aspx.cs源码如下

 


    protected void Page_Load(object sender, EventArgs e)
    {
        Response.CacheControl 
= "no-cache";
        s_rpath 
= Server.UrlDecode(Request.QueryString["Path"]).Replace("\\","\\\\") ;
        s_rppath 
= Server.UrlDecode(Request.QueryString["ParentPath"]).Replace("\\","\\\\");
        
if (Request.QueryString["type"!= null)
        {
            
if (Request.QueryString["type"== "mov")
            {
                DataTable dt 
= Sys.BasePramStart();
                
string Flvdir = dt.Rows[0]["flvsavepath"].ToString();
                
string Datedir = DateTime.Now.ToString("yy-MM-dd"); ;
                
string updir = Flvdir + s_rpath;
                
if (this.Page.Request.Files.Count > 0)
                {
                    
try
                    {

                        
for (int j = 0; j < this.Page.Request.Files.Count; j++)
                        {

                            HttpPostedFile uploadFile 
= this.Page.Request.Files[j];

                            
if (uploadFile.ContentLength > 0)
                            {
                                
if (!Directory.Exists(updir))
                                {
                                    Directory.CreateDirectory(updir);
                                }
                                
string extname = Path.GetExtension(uploadFile.FileName);
                                
string fullname=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+ DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString();
                                
string filename = uploadFile.FileName;
                                
if (Request.QueryString["recname"== "1")
                                {
                                    filename 
= string.Format("{0}{1}", fullname, extname);
                                }
                                uploadFile.SaveAs(
string.Format("{0}\\{1}", updir, filename));
                            }
                        }
                    }
                    
catch (Exception ex)
                    {
                        Record(
"111", ex.ToString());
                    }

                }
            }
        }

    }

 

SWFUpload源码如下

/Files/icejd/SWFFileUpload.zip

posted on 2009-04-11 11:33  rex.ying  阅读(4324)  评论(2编辑  收藏  举报