how to read the content of the file using Fileupload

前端 未结 2 1087
深忆病人
深忆病人 2021-01-12 08:18

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

For example I want to save the XML file in Database , a user search the file using Fileupload and then

相关标签:
2条回答
  • 2021-01-12 08:51

    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

    0 讨论(0)
  • 2021-01-12 09:02
    string inputContent;
    using (StreamReader inputStreamReader = new StreamReader(InputFileUpload.PostedFile.InputStream))
    {
        inputContent = inputStreamReader.ReadToEnd();
    }
    
    0 讨论(0)
提交回复
热议问题