FrenkyB
FrenkyB

Reputation: 7207

Download file from server showing save as dialog

I am facing the problem with downloading files from server (from browser). I have a link of the file and file name. Files can vary - they can be text files or something else.

This is not working any more - it just returns as empty file:

<a href="/resources/files/test.txt" download="">test.sql</a>

I am searching for answer for hours - why is this so hard to achieve? I am willing to do this with javascript action, just don't know how to implement?

Edit: I don't receive any error - link to the file is correct. File returns empty. This is what I see in the browser:

enter image description here

The solution was more than simple:

Server side:

 public FileResult Download(string ImageName)
    {
        var FileVirtualPath = "~/App_Data/uploads/" + ImageName;
        return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));
    }

Client side: all that is needed is hyperlink for that action on the server. Something like:

<a href='/Common/Download?ImageName=test.txt'>test.txt</a>

Upvotes: 1

Views: 110

Answers (1)

FrenkyB
FrenkyB

Reputation: 7207

The solution was more than simple:

Server side:

 public FileResult Download(string ImageName)
    {
        var FileVirtualPath = "~/App_Data/uploads/" + ImageName;
        return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));
    }

Client side: all that is needed is hyperlink for that action on the server. Something like:

<a href='/Common/Download?ImageName=test.txt'>test.txt</a>

Upvotes: 1

Related Questions