文件下载报错:引发类型为“System.OutOfMemoryException”的异常-.Net 内存溢出

CSDN:http://blog.csdn.net/huwei2003/article/details/53559272

 设置了也没有用,于是想到手动清理应用程序池,但又迁配置问题于是改成最后的方式!

 

protected void StartStopRecycleApp(string method)
    {
        string AppPoolName = this.tbAppName.Text.Trim();
        //string method = "Recycle";

        try
        {
            DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
            DirectoryEntry findPool = appPool.Children.Find(AppPoolName, "IIsApplicationPool");
            findPool.Invoke(method, null);
            appPool.CommitChanges();
            appPool.Close();
            lbMsg.Text = string.Format("应用程序池{0}{1}成功", AppPoolName,method);
        }
        catch (Exception ex)
        {
            lbMsg.Text = string.Format("应用程序池{0}{2}失败:{1}", AppPoolName, ex.Message,method);
        }
    }

tbAppName是一个textbox,用来输入应用程序池的名字,如“DefaultAppPool”。
当method="Recycle"时就是回收,为“Start”时是启动,为“Stop”时是停止。

注意:
1. 必须引入System.DirectoryServices包
2. 运行此程序的应用程序也的用户必须权限比较高,可以单独为此程序提供应用程序程,或者建立一个虚拟目录在配制里模拟高级用户(如administrators或者system),否则应用程序会抛出“拒绝访问”的异常。

 

var filePath=Server.MapPath("~/_UploadFile/" + paths[0]);
            if (File.Exists(filePath))
            {
                try
                {
                    FileInfo info = new FileInfo(filePath);
                    long fileSize = info.Length;
                    HttpContext.Current.Response.Clear();

                    //指定Http Mime格式为压缩包
                    HttpContext.Current.Response.ContentType = "application/x-zip-compressed";

                    // Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下:
                    // Content-Disposition: attachment;filename=filename.txt
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(paths[1], System.Text.Encoding.UTF8));
                    //不指明Content-Length用Flush的话不会显示下载进度   
                    HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
                    HttpContext.Current.Response.TransmitFile(filePath, 0, fileSize);
                    HttpContext.Current.Response.Flush();
                }
                catch
                { }
                finally
                {
                    HttpContext.Current.Response.Close();
                }

                //byte[] buffer = null;

                //using (FileStream stream = new FileStream(Server.MapPath("~/_UploadFile/" + paths[0]), FileMode.Open, FileAccess.Read))
                //{
                //    if (stream.Length > 1000000) {
                //        StartStopRecycleApp("Recycle");
                //    }
                //    buffer = new byte[stream.Length];
                //    stream.Read(buffer, 0, buffer.Length);
                //    stream.Close();
                //}
                //if (buffer != null)
                //{
                //    Response.Clear();
                //    Response.Charset = "gb2312";
                //    Response.ContentType = "application/octet-stream";
                //    Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(paths[1], System.Text.Encoding.UTF8));
                //    Response.AddHeader("Content-Length", file.fileSize.ToString());
                //    Response.BinaryWrite(buffer);
                //    Response.Flush();
                //    Response.End();
                //}
            }

 

posted @ 2017-08-24 17:47  三瑞  阅读(2608)  评论(0编辑  收藏  举报