Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8596323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:38:57+00:00 2026-06-12T00:38:57+00:00

public ActionResult GetFile(string dateStr, string serverName, string foodName) { using (var memoStream = new

  • 0
public ActionResult GetFile(string dateStr, string serverName, string foodName)
{
        using (var memoStream = new MemoryStream(1024 * 5))
        {
            using (StreamWriter writer = new StreamWriter(memoStream))
            {
                var dataFilter = new CapacityDataFilter(dateStr, serverName, feedName);
                dataFilter.FilterDataByServerAndFeed();
                writer.WriteLine("Feed, StreamMin, TotalMsgNumber, TotalMsgSize, PeakRateMsgNumber, PeakRateMsgSize");

                foreach (var element in dataFilter.DataInTheDay)
                {
                    writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}",
                            element.Feed, element.StreamMin,
                            element.TotalMsgNumber, element.TotalMsgSize,
                            element.PeakRateMsgNumber, element.PeakRateMsgSize));
                }


                return File(memoStream, "text/csv", fileName);
            }
        }
}

Exception Details: System.ObjectDisposedException: Cannot access a closed Stream.

this action doesn’t work, how to make a download action?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:38:58+00:00Added an answer on June 12, 2026 at 12:38 am

    “this action not work” is wrong way to explain problems…

    But in this case you are lucky: you are trying to send incomplete data in memory stream that is seeked to the end, and as second issue stream will be disposed by the time File action actually executes.

    The best fix is to move code outisde inner using and return new MemoryStream on buffer of old stream:

    using (var memoStream = new MemoryStream(1024 * 5)) 
    { 
       using (StreamWriter writer = new StreamWriter(memoStream)) 
       { 
    ...
       }
       return File(
          new MemoryStream(memoStream.GetBuffer(), memoStream.length),
          "text/csv", fileName); 
    }
    

    Flush + Seek as proposed by armen.shimoon would work too in other cases when you are using stream immediately:

    writer.Flush();
    memoStream.Flush();
    memoStream.Position = 0;    
    // do something with MemoryStream right here before either of `using`ends.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public ActionResult myItems() { var dataContext = new RecordsDataContext(); MembershipUser myObject = Membership.GetUser(); string
public ActionResult Index(){ var dataContext = new DataEvidencijaDataContext(); MembershipUser myObject = Membership.GetUser(); string KorisnickoIme
public class HomeController : Controller { public ActionResult Index() { Task<string> t = DownloadStringTaskAsync(new
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Upload(Photo photo) { foreach (string file in Request.Files) { var path
I have a post action Import: public ActionResult Import() { var fileNames = new
public ActionResult AlbumList3( { AlbumService albumservice = new AlbumService(); var PagingViewModel = new PagingViewModel<Album>
Controller public ActionResult GetCategories() { var htmlText = new StringBuilder(); var scriptText = new
public ActionResult DownloadMembers() { MembershipUserCollection users = Membership.GetAllUsers(); var grid = new WebControls.GridView(); grid.DataSource
public ActionResult MyFile(string MetaValue,int OrganizationId=0) { OrganizationUsersDataContext OrgMeta = new OrganizationUsersDataContext(); JobsRepository JobsRespository =
public ActionResult GeneratePdf(int id) { var labelRepository = new LabelRepository(); var label = labelRepository.GetLabel(id);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.