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

  • SEARCH
  • Home
  • 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 1026923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:09:30+00:00 2026-05-16T12:09:30+00:00

I have a controller action declared as follows: [Authorize(Order = 0, Roles = Requester,Controller,Installer)]

  • 0

I have a controller action declared as follows:

[Authorize(Order = 0, Roles = "Requester,Controller,Installer")]
public FileStreamResult ExportJobCards()

The body of this method builds a collection of CSV lines, and attempts to return them as a file as follows:

using (var sw = new StreamWriter(new MemoryStream()))
{
    foreach (var line in lines)
    {
        sw.WriteLine(line);
    }
    return new FileStreamResult(sw.BaseStream, "text/csv");
}  

When I request this action using the following action link…

Html.ActionLink("Export to Excel", "ExportJobCards")

…the export method executes properly, i.e. all the required CSV data is present in the lines collection in the above code, but I get a File Not Found error rendered as the end result.

EDIT:
In agreement with Tommy’s observation, I moved the return out of the using, and I now get a file, but the file is empty. The new code that actually produces a file, ableit empty, is:

var sw = new StreamWriter(new MemoryStream());            
foreach (var line in lines)
{
    sw.WriteLine(line);
}
sw.Flush();
return new FileStreamResult(sw.BaseStream, "text/csv");  
  • 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-05-16T12:09:30+00:00Added an answer on May 16, 2026 at 12:09 pm

    With your current setup, the Using statement is disposing of the StringWriter before the return can complete, which is resulting in the null reference/file not found error. Remove the using statement or set the StringWriter to another variable before you exit out and you should be good to go on getting rid of the File Not Found error.

    A thought on your second issue now, looking into memorystreams as filestream results, you may need to change your return to this

    sw.BaseStream.seek(0, SeekOrigin.Begin)
    return new FileStreamResult(sw.BaseStream, "text/csv"); 
    

    as the pointer is still at the end of the stream when you return.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller action like this: public ActionResult Index(string url) { var pageTitle
I have an action in my controller as follows. // // GET: /HostingAgr/Details/1 public
I have a controller action that checks this.User.Identity.IsAuthenticated What do you suggest how to
This is working, but how??? I have a controller action for a post: [AcceptVerbs(HttpVerbs.Post
I have the following controller action: [HttpGet] public JsonpResult getTestValues(int? entityId, int? id) {
I have a controller action which renders a partial view: public ActionResult Details(int id)
I have webapi controller with 2 action methods like so: public List<AlertModel> Get() {
I have controller with action new , and I want it to create ActiveRecord::Base
I have a controller action that builds a pdf and than downloads it by
If i have a Controller Action that may recieve both HTTP GET and HTTP

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.