檔案下載報錯:引發類型為“System.OutOfMemoryException”的異常-.Net 記憶體溢出

來源:互聯網
上載者:User

標籤:exce   應用程式   protected   ext   運行   final   清理   star   local   

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();                //}            }

 

檔案下載報錯:引發類型為“System.OutOfMemoryException”的異常-.Net 記憶體溢出

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.