When the file was uploaded to the server following error occurred:
System.IO.DirectoryNotFoundException: Could not find a part of the path C:\TMP\A.csv
In code I'm using postedfile.filename to get the filename and path. But it returns local path. I think the problem is that the fileupload control gets the path from the local machine, "C:\TMP" or whatever which is a local drive and same path / file might not exist at server side.
string strCSVFile = fUCSVFile.PostedFile.FileName.ToString(); // This returns path of the posted file ; i.e client path
System.IO.StreamReader reader = new System.IO.StreamReader(strCSVFile);
reader.Peek();
// Add the values in the DataTable By choosing the values which are separated by Comma
while (reader.Peek() > 0)
{
string words = reader.ReadLine();
/* Processing */
}
On local machine the code was working fine because sever and client are on SAME system.Hence it worked.
After changing the code like below, it started working fine on remote server too.
strCSVFile =Server.MapPath("~") + "\\" + System.Guid.NewGuid().ToString().Replace("-", "");
fUCSVFile.PostedFile.SaveAs(strCSVFile); // File is stored on the Server.
System.IO.StreamReader reader = new System.IO.StreamReader(strCSVFile);
reader.Peek();
// Add the values in the DataTable By choosing the values which are separated by Comma
while (reader.Peek() > 0)
{
string words = reader.ReadLine();
/* Processing */
}
Conclusion:
One should not use "PostedFile.FileName" for accessing the file; though it could be used for getting the filename.The problem of using "PostedFile.FileName" would be difficult to trace if the localpath exists on the server also.
The file should be saved by using "PostedFile.SaveAs(....)" and then accessed.
Hope it Helps.
No comments:
Post a Comment