ASP.NET下载服务器以外的文件

最近在做一个项目,需求是这样的,通过网站能够下载虚拟目录下外的文件。如果是虚拟目录以内的文件,直接给个链接地址就可以,对于以外的,就要采用文件流的方式。我最初采用的是FileStream,在页面上输Byte[]型数据,这种方法可以下载极小的文件,但一遇到大一点的文件就会出现内存不足的错误。后来换成了FileInfo,这种方式可以下载大的文件,但局限于开发环境,一旦打包安装后也会出现页面无法显示的错误。后来换成了以下方式,就没出错了。

                    string thefilename = Server.UrlDecode(Request.QueryString["TheFileName"].ToString());                   
                    string rootpath = ConfigurationManager.AppSettings["RootPath"].ToString();
                    string filepath = Request.QueryString["userID"].ToString() + @"/Download/" + thefilename;
                    string realpath = rootpath + @"/" + filepath;
                    FileInfo fi = new FileInfo(@realpath);            
                    Context.Response.Clear();
                    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 name.
                    string filename = System.IO.Path.GetFileName(realpath);
                    if (filename.Length >= 1)
                    {
                        try
                        {
                            // Open the file.
                            iStream = new System.IO.FileStream(realpath, System.IO.FileMode.Open,
                            System.IO.FileAccess.Read, System.IO.FileShare.Read);
                            // Total bytes to read:
                            dataToRead = iStream.Length;                       
                            Context.Response.AddHeader("Pragma", "public");
                            Context.Response.AddHeader("Content-Description", "File DownLoad");
                            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(thefilename));
                            Response.ContentType = "application/force-download";

                            // 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)
                        {                               
                            Response.Write("Error : " + ex.Message);
                        }
                        finally
                        {
                            if (iStream != null)
                            {                                   
                                iStream.Close();
                                Context.Response.End();
                            }
                        }
                    }                       
                }                                     
                catch (Exception ex)
                {
                    Response.Write(ex.Message + "!!!!!!!".ToString());
                }
            }        

发布了38 篇原创文章 · 获赞 3 · 访问量 5万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章