Girish K G
Girish K G

Reputation: 417

How to get absolute path for file download from another drive?

I need to give a download link to a pdf file which is in D: drive.

My website is hosted in C: drive of same system.

How can i give a download link to my pdf file in D: drive in my website which is hosted in C: drive?

Upvotes: 0

Views: 3839

Answers (2)

Uwe Keim
Uwe Keim

Reputation: 40736

Usually, I try to never expose downloadable files directly.

Instead I write an ASHX handler ("Generic Handler" in Visual Studio) that fetches and sends the file to the user's browser.

Basically, you pass a unique ID to the handler (e.g. the pure file name) and the handler does the rest to fetch the file locally and stream it to the browser like in this pseudo code:

public class MyHandler :
    IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        var fileName = Request[@"fn"];
        var filePath = Path.Combine(@"C:\My\Fixed\File\Path", fileName );

        Response.ContentType = @"application/pdf";

        Response.AddHeader(
            @"Content-Disposition", 
            @"attachment; filename=" + Path.GetFileName(filePath));
        Response.AddHeader(
            @"Content-Length",
            new FileInfo(filePath).Length );

        Response.WriteFile(filePath);
        Response.End();
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

The advantages of such an approach is that you have full control over whether and how the file gets downloaded. Some scenarios include:

  • Permission checking whether user is logged in and may download the file.
  • Streaming with different file names to the client.
  • Logging file download to a database.
  • ...

Upvotes: 1

foxy
foxy

Reputation: 7672

You can mount the drive that is currently D: to a folder inside your C: drive using NTFS junction points, which will make your D: drive appear as though it is part of your C: drive. All problems solved.

Usage details for mklink, a program included with Windows that facilitates the creation, management and deletion of all kinds of links (including junction points), can be found at the Microsoft TechNet documentation center.

Alternatively, you may be interested in symbolic links and hard links, which are similar in nature (though have subtle differences).

Upvotes: 0

Related Questions