当前位置:编程学习 > C#/ASP.NET >>

asp.net超大文件上传方法

asp教程.net超大文件上传方法

asp.net教程大文件上传是一个不完善和有缺陷的领域,相信在不久会得到提高和发展,如果你已经解决了,说明你在一个好公司,否则你可以考虑使用第三方产品来解决了。文件上传的问题,我们都能够找到很多种不同的方法来解决,挑战在于找出不同做法的利弊然后找到一个适用于自己项目的方案,这不仅仅是在文件上传这一个方面!

 

<asp:image id="imglogo" runat="server" /><br/>
<asp:label id="labmsg" runat="server" forecolor="#cc0000"></asp:label><br />
<asp:fileupload id="uploadlogo" runat="server" width="60%" height="22px"/>   
<asp:button id="btnupload" runat="server" text=" 上传 " onclick="btnupload_click"/>

aspx.cs页面

// 上传按钮

protected void btnupload_click(object sender, eventargs e)
{
    uploadfile uploadfileobj = new uploadfile(); //实例化文件上传类
    uploadfileobj.maxfilesize = 100;  // 设置上传文件最大长度,单位k
    uploadfileobj.filetype = "jpg|jpeg|gif|png";  // 设置允许上传的文件类型

    string uploadpath = server.mappath("~/uploadfiles/other/"); // 设置上传目录全路径
    uploadfileobj .uploadfilego(uploadpath, uploadlogo); //文件上传
    labmsg.text = uploadfileobj.uploadinfo; // 上传消息提示
    if (uploadfileobj.uploadstate == true)
    {
        imglogo.imageurl = "~/uploadfiles/other/" + uploadfileobj.newfilename; // 显示图片
    }
   
}

uploadfile.cs 文件上传类

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.io;

namespace common
{
    /// <summary>文件上传类</summary>
    public class uploadfile
    {

        #region 字段

        private string _uploadinfo; // 文件上传的返回信息。
        private bool _uploadstate; // 文件上传的返回状态。
        private string _filetype; // 允许上传文件的类型。
        private int _filesize; // 上传文件的大小,单位b
        private int _maxfilesize; // 上传文件大小的最大长度,单位b
        private string _newfilename; // 上传后的文件名。

        #endregion

 

        /// <summary>初始文件上传类(默认)</summary>
        public uploadfile()
        {
            _uploadinfo = "none";
            _uploadstate = false;
            _filetype = "*";
            _maxfilesize = 1024000;//1000k 即1024*1000b,单位b
            _newfilename = "";
        }
       
        #region 属性
        /// <summary>文件上传的返回信息</summary>
        public string uploadinfo
        {
            set { _uploadinfo = value; }
            get { return _uploadinfo; }
        }

        /// <summary>文件上传的返回状态,成功true,失败false</summary>
        public bool uploadstate
        {
            set { _uploadstate = value; }
            get { return _uploadstate; }
        }

        /// <summary>允许上传文件的类型,* 默认代表可任意类型,或自定义类型,如 "jpg|gif|bmp"</summary>
        public string filetype
        {
            set { _filetype = value; }
            get { return _filetype; }
        }

        /// <summary>上传文件的大小,单位k</summary>
        public int filesize
        {
            get { return _filesize / 1024; }
        }

        /// <summary>上传文件大小的最大长度,单位k</summary>
        public int maxfilesize
        {
            set { _maxfilesize = value * 1024; }
            get { return _maxfilesize / 1024; }
        }

        /// <summary>上传后的文件名</summary>
        public string newfilename
        {
            set { _newfilename = value; }
            get { return _newfilename; }
        }

        #endregion


        #region 上传主程序
        /// <summary>上传本地文件到服务器</summary>
        /// <param name="strsavedir">在服务器端保存的物理路径。</param>
        /// <param name="fileuploadctrlid">上传的文件对象,这里使用的是fileupload控件,</param>
        /// <param>第二个参数如果是html input(file)控件可改为:htmlinputfile htmctrlobjuploadfile</param>
        /// <returns></returns>
        public void uploadfilego(string strsavedir, fileupload fileuploadctrlid)
        {
            int intfileextpoint = fileuploadctrlid.postedfile.filename.lastindexof("."); //最后一个 . 号的位置
            string strfileextname = fileuploadctrlid.postedfile.filename.substring(intfileextpoint + 1).tolower(); // 获取上传文件的后缀名。

            _filesize = fileuploadctrlid.postedfile.contentlength;//上传的文件大小 byte

            if (_filesize == 0) // 判断是否有文件需要上传或所选文件是否为0字节。
            {
                _uploadinfo = "没有选择要上传的文件或所选文件大小为0字节";
                _uploadstate = false;
                return; // 返回文件上传状态和信息。
            }

            if (_filesize > _maxfilesize) // 限制要上传的文件大小(byte)。
            {
                _uploadinfo = "上传的文件超过限制大小(" + (_maxfilesize / 1024).tostring() + "k)";
                _uploadstate = false;
                return; // 返回文件上传状态和信息。
            }

            if (_filetype != "*")
            {
                if (_filetype.tolower().indexof(strfileextname.tolower().trim()) == -1) // 判断要上传的文件类型的是否在允许的范围内。
                {
                    _uploadinfo = "不允许上传的文件类型(允许的类型:|" + _filetype + ")";
                    _uploadstate = false;
                    return; // 返回文件上传状态和信息
                }
            }

            if (_newfilename == "")
            {
                datetime dtenow = datetime.now; // 定义日期对象,为上传后的文件命名。
                _newfilename = dtenow.year.tostring() + dtenow.month.tostring() + dtenow.day.tostring() + getrandomstr(8); // 随机地为上传后的文件命名,日期+随机数。
                _newfilename = _newfilename + "." + strfileextname; //包含扩展名的文件名
            }
            fileuploadctrlid.postedfile.saveas(this.getsavedirectory(strsavedir) + _newfilename); // 以新的文件名保存上传的文件到指定物理路径。          
            _uploadinfo = "文件上传成功"; // 返回上传后的服务器端文件物理路径。
            _uploadstate = true;

        }
        #endregion

 

        /// <summary>获取指定位数的随机数</summary>
        /// <param name="rndnumcount">随机数位数。</param>
        /// <returns></returns>
        private string getrandomstr(int rndnumcount)
        {
            string randomstr;
            randomstr = "";
            random rnd = new random();
            for (int i = 1; i <= rndnumcount; i++)
            {
                randomstr += rnd.next(0, 9).tostring();
            }
            return randomstr;
        }

 


        /// <summary>获取上传文件存放目录</summary>
        /// <param name="directorypath">存放文件的物理路径。</param>
        /// <returns>返回存放文件的目录。</returns>
        public string getsavedirectory(string directorypath)
        {
            if (!directory.exists(directorypath)) // 判断当前目录是否存在。
            {
                directory.createdirectory(directorypath); // 建立上传文件存放目录。
            }
            return directorypath;
        }

    }

 

    #region 附:修改上传大小的配置
    /*
    需要修改的是
    在 c:windowsmicrosoft.netframeworkv1.1.4322config目录里,
    找到文件maxrequestlength="4096"
    将值修改大一些,例如:102400
    这个参数的单位应该是kb的

    以上方法是修改全局的,如果公需要修改一个项目,那么是修改项目里的web.config文件

    在<system.web></system.web>之间添加,
    <httpruntime usefullyqualifiedredirecturl="true" maxrequestlength="21000" executiontimeout="300" />
    其中,
    maxrequestlength:设置上传文件的最大值,单位:kb。(默认是4096kb,即4m)
    executiontimeout:设置超时时间,单位:秒。(默认是90秒)
    */
    #endregion
}

neatupload是在asp.net pipeline的beginrequest事件中截获当前的httpworkerrequest对象,然后直接调用其readentitybody等方法获取客户端传递过来的数据流,并加以分析和处理。并通过使用新的请求进行轮询来获取当前上传的状态。关于neatupload和其他开源组件的介绍可以参看jeffreyzhao的在asp.net应用程序中上传文件,当然他还说了memba velodoc xp edition和swfupload

补充:asp.net教程,.Net开发 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,