如何在文本框中查看文件内容? [英] how to view file content in textbox?

查看:109
本文介绍了如何在文本框中查看文件内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我使用asp.net中的文件上传框选择了一些文本文件...我想查看在另一个文本框中选择的文件文本!!!

Actually i selected some text file using file upload box in asp.net...i wanna view that selected file text in another one textbox!!! how can i view select file content in textbox?

推荐答案

我为此找到解决方案...
i find solution for this...
write this coding on first page in some control like button...

        StreamReader r = new StreamReader(FileUpload1.FileContent);
        content=r.ReadToEnd();
        Session["filecontent"] = content;



然后您想在另一个页面中显示您选择的文件内容textboxt(在多模式下)意味着
将此代码写在第二页上:

Texbox1.Text =会话[文件"] .ToString();


快乐编码:)



then u wanna show ur selected file content in another page textboxt(In multimode) means
write this coding on second page:

Texbox1.Text = Session["files" ].ToString();


Happy Coding :)


您可以将上传的文件输入读取到流对象中,然后使用streamreader读取并在循环显示字符串的末尾在字符串生成器中添加行像这样的texbox中构建器内容:

You can read uploaded file inputsteam into a stream object then using streamreader read and append lines in a string builder at the end of loop display string builder content in a texbox like this:

<table>
          <tr>
              <td>
              </td>
              <td>
                  <asp:FileUpload ID="fileupload1" runat="server" />
              </td>
          </tr>
          <tr>
              <td>
              </td>
              <td>
                  <asp:Button ID="btnRead" runat="server" OnClick="btnRead_Click" Text="Read" />
              </td>
          </tr>
          <tr>
              <td valign="top">
                  Message of Created Text file :
              </td>
              <td>
                  <asp:TextBox ID="textBoxContents" runat="server" TabIndex="0" Height="380px" TextMode="MultiLine"

                      Width="367px"></asp:TextBox>
              </td>
          </tr>
      </table>









protected void btnRead_Click(object sender, EventArgs e)
        {
            string path = fileupload1.PostedFile.FileName;
            if (!string.IsNullOrEmpty(path))
            {

                Stream theStream = fileupload1.PostedFile.InputStream;
                StringBuilder strbuild = new StringBuilder();
                using (StreamReader sr = new StreamReader(theStream))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        strbuild.Append(line);
                        strbuild.AppendLine();
                    }
                }
               
                textBoxContents.Text = strbuild.ToString();
            }

           
        }


这篇关于如何在文本框中查看文件内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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