asp.net-mvcdownloadfileresult

How to generate interchangeable download links?


i'm tying to make DL link so others couldn't dl the same file by sharing it so far i've found this code

public FileResult Download()
{
    byte[] fileBytes = System.IO.File.ReadAllBytes(@"c:\folder\myfile.ext");
    string fileName = "myfile.ext";
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}

it won't make interchangeable links ,how can we do that?


Solution

  • Try this Example:

    public ActionResult Download()
    {
        var filePath=@"c:\folder\myfile.ext";
        var fileBytes = System.IO.File.ReadAllBytes(filePath);
        var response = new FileContentResult(fileBytes, "application/octet-stream")
        {
            FileDownloadName = Path.GetFileName(filePath)
        };
        return response;
    }