zoukankan      html  css  js  c++  java
  • 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());
                    }
                }        

  • 相关阅读:
    oracle本地编译问题
    ORA-214 signalled during: ALTER DATABASE MOUNT 问题
    mysql常用的一些修改命令
    了解PHP中的Array数组和foreach
    在Sublime Text 3上安装代码格式化插件CodeFormatter
    自动在图片上添加页码
    Ubuntu 16.10 安装byzanz截取动态效果图工具
    PHP赋值运算
    Ubuntu 16.10 安装KolourPaint 4画图工具
    PHP数据类型之间的强制转换
  • 原文地址:https://www.cnblogs.com/zhangsongshan/p/2352633.html
Copyright © 2011-2022 走看看