简体   繁体   中英

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:

在此处输入图片说明

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM