Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

Please excuse my lack of knowledge, I am a beginner at best.

I have an application that allows users to upload and download files. I can upload the file to a remote server and display contents of the folder no problem. My problem arises when I try to download, in particular when I try create the correct path. Here is my download code:

What I have tried:

<pre>  public FileResult Download(string ImageName)
        {
            var IdSes = Session["IdSes"];
           string FileVirtualPath= "/Da-app01/Data/Tracker/uploads/" + IdSes +"/"+ ImageName;

            return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));
        }



When I try download a file I get this error:
[DirectoryNotFoundException: Could not find a part of the path 'C:\Users\G\Desktop\application\Da-app01\Data\Tracker\uploads\33\test1.pdf'.]


The problem is the path I need is
Quote:
Da-app01\Data\Tracker\uploads\33\test1.pdf


Where does
Quote:
C:\Users\G\Desktop\application
come from? Can I remove it somehow?

Struggling Here! Any help to understand would be appreciated!
Posted
Updated 8-Sep-17 10:28am

1 solution

Your variable FileVirtualPath, in the eyes of MVC application, is a relative path so your application is apparently in c:\users\g\desktop\application directory so it is concatinating your file location with where it is locally.

In order for the file to download correctly, if it is stored locally on your computer, it has to utilize the full path of where the file is located. So your file isn't located locally on your computer at Da-app-01\Data\Tracker\uploads\33\test1.pdf it has to include the drive path.

Your file needs to reference something like String path = HttpContext.Current.Server.MapPath("~/" + FileVirtualPath); within your controllers action.

You also have options of HostingEnvironment.MapPath("~/" + FileVirtualPath) or Server.MapPath("~/" + FileVirtualPath)
 
Share this answer
 
v2
Comments
Member 13382115 8-Sep-17 17:22pm    
One question before I try the proposed solution. My app is on azure but my files are on a remote file server. Will the solution work?
Graeme_Grant 8-Sep-17 20:16pm    
Yes. It does not hurt to try it, it won't bite!
David_Wimbley 9-Sep-17 0:47am    
If MVC app is on Server 1 and the File is on Server 2, if the MVC application is trying to point to the file locally on Server 2 the only way for that to happen is via shared drive. Or you can expose the files on Server 2 via IIS as HTTP links and access those from Server 1 (the MVC application) using normal file HTTP URLs
Member 13382115 9-Sep-17 4:26am    
Thanks for the reply,
Can you provide a link to some information on how this may be achieved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900