c# - 无法将 HttpPostedFileBase 保存到 session 变量并使用两次

标签 c# asp.net-mvc-2 inputstream httppostedfilebase

早上好- 我正在尝试将 HttpPostedFileBase(它始终是一个简单的 CSV 文件)保存到 session 变量中,如下所示:

    [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");

    }

如您所见,我将 importFile 转储到我的 NoteImport 类中。目前,ImportFile 属性是可公开访问的 HttpPostedFileBase 类型。

我第一次在我的服务中使用这个属性(创建 header 值列表的方法),我没有问题:

    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;
    }

上面的代码工作正常,返回的正是我暂时需要的。

我的问题出在下面的代码上。当我调用 ReadLine() 时,它没有从 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。似乎没有任何进展。我觉得我需要在保存到 session 之前将其更改为不同的类型。

有什么建议吗??

谢谢!

最佳答案

您不能将 HttpPostedFileBase 存储到 session 中。这是没有意义的。它包含指向来自 HTTP 请求流的文件内容的流,该流在请求结束时被垃圾收集。在存储到 session 之前,您需要将其序列化为某个自定义对象。例如:

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

现在继续将其存储到 session 中。当然,您应该知道这样做是将整个文件内容存储到内存中,这可能不是您想要做的事情,尤其是当上传的文件可能很大时。另一种可能性是将文件内容存储到磁盘或数据库等持久性媒体中,并仅将指向该媒体的指针存储到 session 中,以便您以后可以检索它。

关于c# - 无法将 HttpPostedFileBase 保存到 session 变量并使用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4747833/

相关文章:

c# - 如何将一个 ViewModel 转换为另一个 ViewModel?

java - 在不缓冲的情况下从 InputStream 中读取行

java - 如何从设备文件夹中检索和共享位图

c# - 为什么我的 Command.CanExecute 在单元测试中总是返回 false?

c# - 为 64 位 Windows 计算机创建构建时出现 "Access to database file is not allowed"错误

asp.net-mvc - 如何更改 ASP.NET MVC 中的默认验证错误消息?

javascript - DateTime 的 MVC2 客户端验证?

c# - 如何传递 HttpInputStream 的内容并将其发送到 c# .net 中的另一个 Rest Api

c# - 未将对象引用设置为对象的实例 asp.net

c# - 覆盖新的 SqlParameter