C# 如何使用 Fileupload 读取文件内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11052238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:17:18  来源:igfitidea点击:

how to read the content of the file using Fileupload

c#asp.net

提问by Baper

Is it possible to read the content of a file using Fileupload.

是否可以使用 Fileupload 读取文件的内容。

For example I want to save the XML file in Database , a user search the file using Fileupload and then click a button to save the content of the file in Database.

例如,我想将 XML 文件保存在 Database 中,用户使用 Fileupload 搜索文件,然后单击按钮将文件的内容保存在 Database 中。

I tried this one but doesn't work

我试过这个,但不起作用

string s=Fileuploder1.Filecontent.tostring();

but no success,Do you have any idea?

但没有成功,你有什么想法吗?

回答by Chris Schiffhauer

string inputContent;
using (StreamReader inputStreamReader = new StreamReader(InputFileUpload.PostedFile.InputStream))
{
    inputContent = inputStreamReader.ReadToEnd();
}

回答by Mohamed Rasik

we cannot directly read the file, instead of that we should save it in the project location. using the project file path we can read with the help of stream reader.

我们不能直接读取文件,而是应该将其保存在项目位置。使用我们可以在流阅读器的帮助下读取的项目文件路径。

var filePath = Path.Combine(Server.MapPath("~/Document"), fileName);
                file.SaveAs(filePath);

                if (!string.IsNullOrEmpty(filePath))
                {
                    using (StreamReader sr = new StreamReader(Path.Combine(Server.MapPath("~/Document"), fileName)))
                    {
                        while (sr.Peek() >= 0)
                        {
                            strbuild.AppendFormat(sr.ReadLine());
                        }
                    }

                }

for more details:http://www.infinetsoft.com/Post/How-to-read-text-file-using-fileupload-control-in-asp-net-MVC/1245

更多详情:http: //www.infinetsoft.com/Post/How-to-read-text-file-using-fileupload-control-in-asp-net-MVC/1245