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

asp.net 下载大文件时,不能操作其他页面,求解

asp.net 下载大文件时,不能操作其他页面,必须等文件下载完了 才能操作其他页面这个可怎么办 asp.net 下载大文不能操作其他页面 --------------------编程问答-------------------- 遇到过类似的问题,整个局域网的用户都瘫痪了,记得当时是iis设置了啥子就好了,上网看看呢 --------------------编程问答-------------------- 把文件服务器与网站服务器分开 --------------------编程问答--------------------
引用 2 楼 liuchaolin 的回复:
把文件服务器与网站服务器分开
大神除了这个还有别的方案吗, 我写个线程行不行 ,代码
  while (dataToRead > 0)
            {
                // Verify that the client is connected.
                if (Response.IsClientConnected)
                {
                    // Read the data in buffer.
                    length = iStream.Read(buffer, 0, 10000);

                    // Write the data to the current output stream.
                    Response.OutputStream.Write(buffer, 0, length);

                    // Flush the data to the HTML output.
                    Response.Flush();

                    buffer = new Byte[10000];
                    dataToRead = dataToRead - length;
                }
                else
                {
                    //prevent infinite loop if user disconnects
                    dataToRead = -1;
                }
            }
        }

我认为是不是因为这个Response 写的慢 需要时间 那么我可以不可以写个线程 ,但是网页里写线程 我害怕有问题。。。 --------------------编程问答--------------------
引用 1 楼 IT_2007 的回复:
遇到过类似的问题,整个局域网的用户都瘫痪了,记得当时是iis设置了啥子就好了,上网看看呢

怎么设置的,你说的那个是不是直接全部写到内存里 我用的是大文件下载方法   while (dataToRead > 0)
            {
                // Verify that the client is connected.
                if (Response.IsClientConnected)
                {
                    // Read the data in buffer.
                    length = iStream.Read(buffer, 0, 10000);

                    // Write the data to the current output stream.
                    Response.OutputStream.Write(buffer, 0, length);

                    // Flush the data to the HTML output.
                    Response.Flush();

                    buffer = new Byte[10000];
                    dataToRead = dataToRead - length;
                }
                else
                {
                    //prevent infinite loop if user disconnects
                    dataToRead = -1;
                }
            }
        }
是这种 ,服务器倒是不会卡死 就是 下载的时候无法操作其他页面 --------------------编程问答-------------------- 有没有人能帮下忙啊 --------------------编程问答--------------------             DownAdress = dimm + DownAdress;
            if (!File.Exists(Server.MapPath(DownAdress)))
            {
                Response.Write("下载的文件不存在!");
                Response.End();
                return;
            }
            using (FileStream fso = new FileStream(Server.MapPath(DownAdress), FileMode.Open,FileAccess.ReadWrite,FileShare.Inheritable))
            {
                string[] filename=DownAdress.Split(new char[]{'/'});
                int len = Convert.ToInt32(fso.Length);
                byte[] FileObj = new byte[len];
                fso.Read(FileObj, 0, len);
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", HttpUtility.UrlEncode(filename[filename.Length - 1]), System.Text.Encoding.UTF8));
                Response.AddHeader("Content-Length", len.ToString());
                Response.ContentType = "application/octet-stream";
                Response.Charset = "UTF-8";
                Response.ContentEncoding = System.Text.Encoding.UTF8; 
                Response.BinaryWrite(FileObj);
                //Response.Flush();
                //Response.Clear();
                fso.Close();
            } --------------------编程问答-------------------- 用后台线程下载,采用回调函数报告下载进度,并且随时可以取消下载。 --------------------编程问答--------------------
Quote: 引用 6 楼 liuchaolin 的回复:
 string name = "";
        if (Request.Params["name"] != null)
        {
            name = Request.Params["name"].ToString();

            name = Server.UrlDecode(name);

            string ddd = name.Substring(name.Length - 1, 1);
            if (ddd.EndsWith("*"))
            {
                name = name.Substring(0, name.Length - 1);
            }

        }
        System.IO.Stream iStream = null;

        // Buffer to read 10K bytes in chunk:
        byte[] buffer = new Byte[10000];

        // Length of the file:
        int length;

        // Total bytes to read:
        long dataToRead;

        // Identify the file to download including its path.
        string filepath = path;

        // Identify the file name.
        string filename = System.IO.Path.GetFileName(filepath);
        FileInfo filesss = new FileInfo(path);     
        try
        {
            // Open the file.
            iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
                        System.IO.FileAccess.Read, System.IO.FileShare.Read);


            // Total bytes to read:
            dataToRead = iStream.Length;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码           
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
            Response.AddHeader("Content-length", filesss.Length.ToString());  
            // Read the bytes.
            while (dataToRead > 0)
            {
                // Verify that the client is connected.
                if (Response.IsClientConnected)
                {
                    // Read the data in buffer.
                    length = iStream.Read(buffer, 0, 10000);

                    // Write the data to the current output stream.
                    Response.OutputStream.Write(buffer, 0, length);

                    // Flush the data to the HTML output.
                    Response.Flush();

                    buffer = new Byte[10000];
                    dataToRead = dataToRead - length;
                }
                else
                {
                    //prevent infinite loop if user disconnects
                    dataToRead = -1;
                }
            }
        }
        catch (Exception ex)
        {
            // Trap the error, if any.
            Response.Write("Error : " + ex.Message);
        }
        finally
        {
            if (iStream != null)
            {
                //Close the file.
                iStream.Close();
            }
        }
 这个是全代码  你说的那种下载大文件好像有点什么问题我记得,然后我找的这个方法 --------------------编程问答--------------------  string name = "";
        if (Request.Params["name"] != null)
        {
            name = Request.Params["name"].ToString();

            name = Server.UrlDecode(name);

            string ddd = name.Substring(name.Length - 1, 1);
            if (ddd.EndsWith("*"))
            {
                name = name.Substring(0, name.Length - 1);
            }

        }
        System.IO.Stream iStream = null;

        // Buffer to read 10K bytes in chunk:
        byte[] buffer = new Byte[10000];

        // Length of the file:
        int length;

        // Total bytes to read:
        long dataToRead;

        // Identify the file to download including its path.
        string filepath = path;

        // Identify the file name.
        string filename = System.IO.Path.GetFileName(filepath);
        FileInfo filesss = new FileInfo(path);     
        try
        {
            // Open the file.
            iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
                        System.IO.FileAccess.Read, System.IO.FileShare.Read);


            // Total bytes to read:
            dataToRead = iStream.Length;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码           
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
            Response.AddHeader("Content-length", filesss.Length.ToString());  
            // Read the bytes.
            while (dataToRead > 0)
            {
                // Verify that the client is connected.
                if (Response.IsClientConnected)
                {
                    // Read the data in buffer.
                    length = iStream.Read(buffer, 0, 10000);

                    // Write the data to the current output stream.
                    Response.OutputStream.Write(buffer, 0, length);

                    // Flush the data to the HTML output.
                    Response.Flush();

                    buffer = new Byte[10000];
                    dataToRead = dataToRead - length;
                }
                else
                {
                    //prevent infinite loop if user disconnects
                    dataToRead = -1;
                }
            }
        }
        catch (Exception ex)
        {
            // Trap the error, if any.
            Response.Write("Error : " + ex.Message);
        }
        finally
        {
            if (iStream != null)
            {
                //Close the file.
                iStream.Close();
            }
        }
--------------------编程问答-------------------- 普通的小文件 看不出来 但是一涉及到 1g 几百m的 能发现 整个网站卡主了,但是只是限于这个游览器不会影响其他人 --------------------编程问答-------------------- 那是你浏览器的问题了,你可以用迅雷等去下载试试 --------------------编程问答--------------------
引用 11 楼 liuchaolin 的回复:
那是你浏览器的问题了,你可以用迅雷等去下载试试
 不是啊,我就是用迅雷下载的,iis 这方面需要做什么设置吗 --------------------编程问答-------------------- 还有没有人遇见这个问题呢 --------------------编程问答-------------------- 帖子不沉就有希望 --------------------编程问答-------------------- session默认用的是排他锁
http://www.cnblogs.com/OpenCoder/archive/2010/01/10/1643659.html
http://58501040.blog.51cto.com/1332797/282473
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,