Using server.mapath to retrieve a file from a physical path

Last Reply on Mar 30, 2016 11:49 PM By Azim

Posted on Mar 30, 2016 12:20 PM

Trying to read my file at:-

Physical Path    h:\root\home\snookertable-001\www\site1\DisplayLatestNews

from the server where my site is hosted

 

if (!Page.IsPostBack)
            {
                if (System.IO.File.Exists(Server.MapPath("h:\\root\\home\\snookertable-001\\www\\site1\\DisplayLatestNews")))
                {
                    System.IO.StreamReader StreamReader2 = new System.IO.StreamReader(Server.MapPath("h:\\root\\home\\snookertable-001\\www\\site1\\DisplayLatestNews"));
                    TextBox1.Text = StreamReader2.ReadToEnd();
                    StreamReader2.Close();
                }
            }

Just comes back with a non descriptive error, I have used "\\" instead of "\" as it does not like just the one backslash, any ideas please ?

Posted on Mar 30, 2016 09:29 PM

Hi Snooker,

 

Location of the path in  user machine  will not be sent to server

Best Way to do this is ,You need to save the file on the server’s folder and an easiest way would be to create a Folder called Uploads in your Website or Web Application and then saving the file there using the following code

 

string Name = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/" + Name));

Here Name is retrieving ur file name.

 

Hope this helps