第一次读取文件后,能否让StreamReader.EndOfStream返回false? [英] Can I get StreamReader.EndOfStream to return false after the first time I read a file?

查看:180
本文介绍了第一次读取文件后,能否让StreamReader.EndOfStream返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-简短版本:当我第一次在第一次之后进入 while(!checkReader.EndOfStream)时,它说 EndOfStream = true .

---short version: When I get to the while (!checkReader.EndOfStream) every time after the first, it says EndOfStream = true.

---更多详细信息:用户将使用Ajax AsyncFileUpload控件上传文件.我获取该文件,确保它是我们使用的非常特定的csv格式,并将其吐到GridView中.第一次,一切都很好:我得到了文件,将其解析出来,并显示出很好的效果.
但是,如果我在用户会话期间的任何时间再次调用相同的代码,则 StreamReader.EndOfStream = true .

---more detail: A user will upload a file using an Ajax AsyncFileUpload control. I take that file, ensure it's a very specific format of csv that we use and spit it out into a GridView. This all works great the first time through: I get the file, parse it out, and it displays great.
But, if I call this same code again anytime during the user's session the StreamReader.EndOfStream = true.

例如,一个用户上传一个文件,然后将其吐到GridView中.糟糕!用户意识到有标题...我有一个与事件处理程序一起使用的复选框,该处理程序将调用下面的方法以重新读取原始文件(存储在会话变量中).用户选中该框,事件触发,方法被调用,但是我的 EndOfStream 现在为true.

For example, a user uploads a file and I spit it out into the GridView. Oops! User realizes there are headers... I have a checkbox available with an event handler that will call the method below to re-read the original file (it's stored in a session variable). User checks the box, event fires, method gets called, but my EndOfStream is now true.

我认为 using()会更改该标志,并且我尝试在下面的while循环之后添加 checkReader.DiscardBufferedData ,但是这些似乎都没有影响.

I thought that using () would change that flag and I have tried adding checkReader.DiscardBufferedData just after the while loop below, but neither of those seem to have any affect.

我做错了什么?

 private void BuildDataFileGridView(bool hasHeaders)
{   
    //read import file from the session variable
    Stream theStream = SessionImportFileUpload.PostedFile.InputStream;
    theStream.Position = 0;

    StringBuilder sb = new StringBuilder();

    using (StreamReader checkReader = new StreamReader(theStream))
    {
        while (!checkReader.EndOfStream)
        {
            string line = checkReader.ReadLine();
            while (line.EndsWith(","))
            {
                line = line.Substring(0, line.Length - 1);
            }
            sb.AppendLine(line);
        }
    }

    using (TextReader reader = new StringReader(sb.ToString()))
    { 
        //read the file in and shove it out for the client
        using (CsvReader csv = new CsvReader(reader, hasHeaders, CsvReader.DefaultDelimiter))
        {
            sDataInputTable = new DataTable();

            try
            {
                //Load the DataTable with csv values  
                sDataInputTable.Load(csv);
            }
            catch
            {
                DisplayPopupMessage("ERROR: A problem was encountered");
            }

            //Copy only the first 10 rows into a temp table for display.  
            DataTable displayDataTable = sDataInputTable.Rows.Cast<System.Data.DataRow>().Take(10).CopyToDataTable();

            MyDataGridView.DataSource = displayDataTable;
            MyDataGridView.DataBind();
        }
    }
}

SessionImportFileUpload是实际的Ajax AsyncFileUpload控件,它存储为会话变量(在以前的人使用它的其他内容中,这种情况已经存在).

SessionImportFileUpload is the actual Ajax AsyncFileUpload control being stored as a session variable (this was already the case as a previous person wrote other stuff in that uses it).

推荐答案

您正在将发布的文件流存储在Session中.这是不正确的,因为流不是数据,而是读取数据的机制.在单个POST请求期间,该文件仅上传一次,以后您将无法再次从同一流中读取文件.通常,您甚至无法倒带该流以重新读取它.

You are storing the posted file stream in Session. This is not correct, because the stream is not the data, but rather the mechanism to read the data. The file is uploaded only once, during a single POST request, and you won't be able to read from the same stream again later. Usually you even cannot rewind the stream to re-read it.

这就是为什么我建议只读取发布的文件流一次并将整个内容放入Session中的原因-这样,内容将可以重复使用,并且您可以根据需要将其重新处理多次.

That's why I suggest to read the posted file stream only once and put the whole content into Session - this way the content will be reusable, and you'll be able to reprocess it as many times as you need.

这篇关于第一次读取文件后,能否让StreamReader.EndOfStream返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆