0

This is my actionlink on the index view.

@Html.ActionLink("Download File", "Download", new { fileName = Model.OriginalRecordRelativeFilePath })


public FileResult Download(string fileName)
{
     try
     {
          var fs = System.IO.File.OpenRead(Server.MapPath(ConfigurationHelper.IntegrationInterfaceFolderPath + fileName));
     // ConfigurationHelper.IntegrationFolderPath is a path for C:\Data\IntergrationInterface\

     return File(fs, "application/octet-stream", fileName);
 }
 catch
 {
      throw new HttpException(404, "Couldn't find " + fileName);
 }

}

I try to download the file from C:\Data\IntergrationInterface\fileName. But it cannot find the path of C:\Data\IntergrationInterface\fileName and throw exception (Couldnt find fileName). May i know is there any ways to download the file from local c while running localhost web service on the same computer? Thank you.

3
  • The exception doesn't lie. Does the file exist? Verify the path, preferably by storing it in a separate variable. Does the user this site runs as have permissions to read the file? Commented Oct 31, 2014 at 8:49
  • Yes. The file existed. I created a dummy file named path in C:\Data\IntergrationInterface\path. the user this site is coded as system admin. So it has permission to read the file. Commented Oct 31, 2014 at 8:57
  • Change the code to string path = ConfigurationHelper.IntegrationInterfaceFolderPath + fileName; path = Server.MapPath(path); var fs = System.IO.File.OpenRead(path);. Inspect the variables on each step. Commented Oct 31, 2014 at 8:58

1 Answer 1

2

Do not use Server.MapPath here instead use your file path directly. For example @"C:\Data\IntergrationInterface\".

Currently you are trying to do Server.MapPath(@"C:\Data\IntergrationInterface\file.txt"), it means you are giving a physical path where as MapPath method expects a virtual path. Instead of this if you would give Server.MapPath("\ABC") then it will return the server path as "C:\inetpub\wwwroot\ABC", which will be basically your local server path.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.