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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章