Shivkumar Deshmukh
Shivkumar Deshmukh

Reputation: 1148

'Not A Virtual Path',Error While Downloading A File

I am Downloading a file from Server, File is Present on some other disk not in same directory as Project is

my controller code is

public ActionResult GetFileFromDisk()
{
     string PATH = @"E:\10-FEB-2012\DIR1\DIR2";
     return File(Server.MapPath( (PATH + fileName ), type, fileName);
}

if I will not user Server.MapPath Code Working On my machine but when i deploy application on server it gives error

Upvotes: 2

Views: 730

Answers (2)

Kangkan
Kangkan

Reputation: 15571

To be able to download the file, the file location need to be available on the web site. Even if the file is not available in your current app, it will have to be available on a different app. Otherwise you can not download it.

If you have an issue or constraint that you can not put it in the app path and your app can access the file (read), you may read the file in your application and rewrite it on the response.

Upvotes: 1

superwalnut
superwalnut

Reputation: 319

As it is already physical address, you don't need to use mappath to convert it. MapPath is used to convert virtual path to physical path. Can you check on the server do you have that file exist on the drive?? Also check the permissions it may need IIS_User or network_service.

I normally load the file then use stream for the File result. Use the overload of File(Stream stream, string contentType, string fileName)

FileInfo info = new FileInfo(packageItem.PhysicalPath);
                if (info.Exists)
                {
                    return File(info.OpenRead(), System.Net.Mime.MediaTypeNames.Application.Octet, info.Name);
                }

Upvotes: 1

Related Questions