日期:2014-05-18  浏览次数:21014 次

大文件读取!!!急!!!
大文件读取!!!
只想一行行读!!!不想一下子把一个几百M的文件读入!!!!
请问有什么方法。网站都快挂了...

------解决方案--------------------
那就不要用ReadToEnd啦,StreamReader有ReadLine方法,可以读入一行


------解决方案--------------------
几百M的文件,上传的时候按规则划开/....
------解决方案--------------------
如果用FileStream 读取
FileStream fs = new FileStream(...);
byte[] buf = new byte[1024];
int count;
count=fs.Read(0,buf.Length,buf);
配合移动fs.Position可以读取到文件的任何一部分数据
------解决方案--------------------
自己些个IHttpModule,使用HttpWorkerRequest来读取。
所有的asp.net的大附件上传都是这么做的。
有现成的代码可以搜搜
------解决方案--------------------
C# code

#region //大文件读取类
    public class FileSplitSteamReader : FileStream
    {
        #region //文件分块读
        /// <summary>
        /// 用于普通文件读取
        /// </summary>
        /// <param name="sourceFileName">文件的路径</param>
        public FileSplitSteamReader(string sourceFileName)
            : base(sourceFileName, FileMode.Open, FileAccess.Read)
        {
            this.sourceFileName = sourceFileName;
        }
        /// <summary>
        /// 用于大文件读取
        /// </summary>
        /// <param name="sourceFileName">文件的路径</param>
        /// <param name="splitSize">切分大小</param>
        public FileSplitSteamReader(string sourceFileName, int splitSize)
            : base(sourceFileName, FileMode.Open, FileAccess.Read)
        {
            this.sourceFileName = sourceFileName;
            this.splitSize = splitSize;
        }

        private string sourceFileName;
        /// <summary>
        /// 获取文件的路径
        /// </summary>
        public string SourceFileName
        {
            get { return sourceFileName; }
        }

        private long splitSize;
        /// <summary>
        /// 每次切分文件大小splitSize
        /// </summary>
        public long SplitSize
        {
            get { return splitSize; }
        }

        /// <summary>
        /// 文件的大小
        /// </summary>
        public long FileSize
        {
            get { return this.Length; }
        }

        private long readTimes = 1;
        /// <summary>
        /// 当前读取次数块号
        /// </summary>
        public long ReadTimes
        {
            get { return readTimes; }
        }

        private bool judge = false;
        /// <summary>
        /// 用于判断是否执行到最后一块.读完为ture,未读完为false.
        /// </summary>
        public bool Judge
        {
            get { return judge; }
            set { judge = value; }
        }

        /// <summary>
        /// 最后一次读取文件的大小
        /// </summary>
        public long FinilReadSize
        {
            get
            {
                if (splitSize == 0)
                    return 1024 * 1024 * 3;
                else
                    return this.FileSize - (this.FileSize / (long)this.splitSize) * (long)this.splitSize;

            }
        }
        public int CurrentReadSize;
        /// <summary>
        /// 开始读取文件
        /// </summary>
        /// <returns>以Bitmap类型返回每次读取文件的内容</returns>
        public byte[] SpliteRead()
        {
            FileBlockSteamReaderEventArgs Fbsr = new FileBlockSteamReaderEventArgs();
            byte[] timeReadContect;
            this.Seek(splitSize * (readTimes - 1), SeekOrigin.Begin);
            if (readTimes < (this.FileSize / this.splitSize + 1))
            {
                Fbsr.ReadPercent = (int)(((float)this.Position / this.FileSize) * 100);
                timeReadContect = new byte[this.splitSize];
            }
            else
            {
                timeReadContect = new byte[this.FinilReadSize];
                judge = true;
                Fbsr.ReadPercent = 100;