Cannot save HttpPostedFileBase session variable and use twice

Good morning - I'm trying to save an HttpPostedFileBase (which will always be a simple CSV file) for the session variable as follows:

    [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 can see, I'm dumping importFile into my NoteImport class. The ImportFile property is currently a public type of HttpPostedFileBase.

The first time I use this property in my service (a 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 at the moment.

My problem is with the code below. When I call ReadLine (), it does not get anything from 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;
    }

HttpPostedFileBase; 0. , , . , .

!

+5
3

HttpPostedFileBase . . , HTTP-, , . - . :

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

. , , , , , , , . , , , , .

+4

HttpPostedFileBase, ,
git
:

public static class HttpPostedFileBaseExtensions
{
    public static Byte[] ToByteArray(this HttpPostedFileBase value)
    {
        if (value == null)
            return null;
        var array = new Byte[value.ContentLength];
        value.InputStream.Position = 0;
        value.InputStream.Read(array, 0, value.ContentLength);
        return array;
    }
}

HttpPostedFileBase :

private static void doSomeStuff(HttpPostedFileBase file)
{
    try
    {
        using (var reader = new MemoryStream(file.ToByteArray()))
        {
            // do some stuff... say read it to xml
            using (var xmlTextReader = new XmlTextReader(reader))
            {                    

            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

:

file.SaveAs(path);

.

+1

HttpPostedFile HTTP.
.

0

All Articles