1

in my controller, this is the code i have

   public FileResult Download(string file)
    {



        var vFullFileName = HostingEnvironment.MapPath("~/App_Data/Files/");

        var files = uploadedfileRepository.AllIncluding();
        string filename = (from f in files

                           select f.FileName).First();


        return File(Path.Combine(vFullFileName, filename), "application/csv", filename);
    }

I put the breakpoint and the file point to the right directory, but why still give me File not found exception?

and in my view this is what i have

 <td>
            @Html.ActionLink("Download", "Download", new { id = item.FileName})
        </td
cool_spirit
  • 677
  • 3
  • 7
  • 20

2 Answers2

0

App_Data is for database files that are accessed by MS SQL Server, and possibly your application's own data files (like a Lucene index).

IIS (and ASP.NET) are configured to block any client requests to that directory.

The solution is just to move the files to another directory. Just create a new folder in your site's root (say "CsvFiles") and link to that.

That said, why don't you serve up a HTTP 301 Redirection (or even a direct link) to the CSV files instead of serving them via your application?

UPDATE: This answer is incorrect because by serving the file contents via an MVC File response the user's browser doesn't acutally access the App_Data directory.

Dai
  • 141,631
  • 28
  • 261
  • 374
0

Your action method have a parameter with name file. But your are HTML will have a parameter/ query string called id, instead od file, So change your view code to

@Html.ActionLink("Download", "Download", new { file= item.FileName})

Also to get the path, try this

string fullFilePath=Path.Combine(Server.MapPath("~/App_Data"),filename)
return File(fullFilePath,"application/csv",filename);
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • hey Shyju, thanks for reply, even i change the data type to file, the same exception still hangs there – cool_spirit Sep 06 '12 at 20:41
  • check this answer to get the correct way to get path http://stackoverflow.com/questions/1268738/asp-net-mvc-find-absolute-path-to-the-app-data-folder-from-controller – Shyju Sep 06 '12 at 20:44
  • so the path instead using direct access to App_Data/Files, use server.mappath to access it, how about my parameter, can i leave it using "string file" ? – cool_spirit Sep 06 '12 at 20:49
  • If you keep your parameter name as file in your action method ( that is what you have in your question), you need to call it with 'file` querystring. Use the ActionLink sample i wrote in my answer. – Shyju Sep 06 '12 at 20:50
  • the exception still hangs there after i apply your method, i put the break point there and parameter "string file" show the file name and fullFilePath shows the whole full path, and exception still filenotfound, url somehow doesnt map to the link/dir – cool_spirit Sep 06 '12 at 21:00
  • make sure the file has READ permission for IIS users/ ASP.NET – Shyju Sep 06 '12 at 21:08
  • right click on folder, select security tab, edit/add users with relevant permissions – Shyju Sep 06 '12 at 21:11