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 3782690
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:07:07+00:00 2026-05-19T11:07:07+00:00

Good morning – I am attempting to save an HttpPostedFileBase (which will always be

  • 0

Good morning –
I am attempting to save an HttpPostedFileBase (which will always be a simple CSV file) to a session variable like so:

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase importFile) {
        Session.Remove("NoteImport");
        var noteImport = new NoteImport { ImportFile = importFile, HeaderList = NoteService.HeaderList(importFile) };
        Session["NoteImport"] = noteImport;
        return RedirectToAction("FileNote");

    }

As you see, I am dumping importFile into my NoteImport class. Currently, the ImportFile property is a publicly accessible HttpPostedFileBase type.

The first time I use this property in my service (method that creates a list of header values), I have no problem:

    public List<string> HeaderList(HttpPostedFileBase fileStream) {
        var sr = new StreamReader(fileStream.InputStream);
        var headerString = sr.ReadLine();
        var headerArray = headerString.Split(',');
        var headerList = new List<string>();
        foreach (var header in headerArray) {
            if (!ValidateHeader(header))
                throw new InvalidDataException("Invalid header name: " + header);
            headerList.Add(header);
        }
        return headerList;
    }

The above works fine and returns exactly what I need for the time being.

My problem is with the code below. When I call ReadLine(), it doesn’t get anything out of the HttpPostedFileBase.

    public List<string> ImportFileStream(HttpPostedFileBase importFile) {
        var sr = new StreamReader(importFile.InputStream);
        var headerString = sr.ReadLine();
        var headerArray = headerString.Split(',');
        var cb = new DelimitedClassBuilder("temp", ",") { IgnoreFirstLines = 0, IgnoreEmptyLines = true, Delimiter = "," };
        foreach (var header in headerArray) {
            cb.AddField(header, typeof(string));
            cb.LastField.FieldQuoted = true;
            cb.LastField.QuoteChar = '"';
        }
        var engine = new FileHelperEngine(cb.CreateRecordClass());
        var dataTable = engine.ReadStreamAsDT(sr);
        var noteData = new List<string>();
        var jsonSerializer = new JsonSerializeDataRow();
        foreach (var row in dataTable.Rows) {
            var dataRow = row as DataRow;
            var jsonRow = jsonSerializer.Serialize(dataRow);
            noteData.Add(jsonRow);
        }
        return noteData;
    }

I have attempted closing the HttpPostedFileBase; I have also set the stream position to 0. Nothing seems to be going. I have a feeling I need to change it to a different type before saving to the session.

Any advice??

Thanks!

  • 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-19T11:07:07+00:00Added an answer on May 19, 2026 at 11:07 am

    You cannot store an HttpPostedFileBase into the session. This just doesn’t make sense. It contains a stream pointing to the file contents from the HTTP Request stream which is garbage collected at the end of the request. You need to serialize it to some custom object before storing into session. For example:

    public class MyFile
    {
        public string Name { get; set; }
        public string ContentType { get; set; }
        public byte[] Contents { get; set; }
    }
    

    Now go ahead and store this into the session. Of course you should be aware that by doing this you are storing the entire file contents into memory which might not be something you want to do especially if the uploaded files can be large. Another possibility is to store the file contents into the persistable media like a disk or database and store into session only a pointer to this media so that you can retrieve it later.

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

Sidebar

Related Questions

Good morning. I have an XML file which contains lists of warning and errors
Good morning, I am the developer of a medium sized PDA application that will
Good morning. Take this simple scenario: I've got a NetClient that connects itself to
Good morning in my timezone. Those are simple questions , i just do not
Good morning, I would like the code in my controller to look something like
Good morning! We have a SVN-Repository, which holds a VS-Solution. In this solution are
Good morning, I´m facing a very strange problem on which I haven´t found a
Good Morning, I have an application which allows users to slide values for different
Good morning stackoverflow ! I'm trying to extract from a binary file a hexadecimal
Good morning, I'm trying to connect to a site using SSL and always receive

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.