如何从服务器下载文件到客户端?

时间:2022-11-08 03:38:17

I have an MVC project where I'd like the user to be able to download a an excel file with a click of a button. I have the path for the file, and I can't seem to find my answer through google.

我有一个MVC项目,我希望用户能够通过单击按钮下载excel文件。我有文件的路径,我似乎无法通过谷歌找到我的答案。

I'd like to be able to do this with a simple button I have on my cshtml page:

我希望能够通过我在cshtml页面上的一个简单按钮来完成此操作:

<button>Button 1</button>

How can I do this? Any help is greatly appreciated!

我怎样才能做到这一点?任何帮助是极大的赞赏!

2 个解决方案

#1


14  

If the file is not located inside your application folders and not accessible directly from the client you could have a controller action that will stream the file contents to the client. This could be achieved by returning a FileResult from your controller action using the File method:

如果文件不在您的应用程序文件夹中,并且无法直接从客户端访问,则可以使用控制器操作将文件内容流式传输到客户端。这可以通过使用File方法从控制器操作返回FileResult来实现:

public ActionResult Download()
{
    string file = @"c:\someFolder\foo.xlsx";
    string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    return File(file, controntType, Path.GetFileName(file));
}

and then replace your button with an anchor pointing to this controller action:

然后用指向此控制器操作的锚点替换您的按钮:

@Html.ActionLink("Button 1", "Download", "SomeController")

Alternatively to using an anchor you could also use an html form:

或者使用锚点,您也可以使用html表单:

@using (Html.BeginForm("Download", "SomeController", FormMethod.Post))
{
    <button type="submit">Button 1</button>
}

If the file is located inside some non-accessible from the client folder of your application such as App_Data you could use the MapPath method to construct the full physical path to this file using a relative path:

如果文件位于应用程序的客户端文件夹(例如App_Data)中的某些不可访问的文件中,则可以使用MapPath方法使用相对路径构造此文件的完整物理路径:

string file = HostingEnvironment.MapPath("~/App_Data/foo.xlsx");

#2


2  

HTML:

<div>@Html.ActionLink("UI Text", "function_name", "Contoller_name", new { parameterName = parameter_value },null) </div>

Controller:

public FileResult download(string filename) {
        string path = "";
        var content_type = "";
        path = Path.Combine("D:\file1", filename);

        if (filename.Contains(".pdf"))
        {
            content_type = "application/pdf";
        }
        return File(path, content_type, filename);
}

#1


14  

If the file is not located inside your application folders and not accessible directly from the client you could have a controller action that will stream the file contents to the client. This could be achieved by returning a FileResult from your controller action using the File method:

如果文件不在您的应用程序文件夹中,并且无法直接从客户端访问,则可以使用控制器操作将文件内容流式传输到客户端。这可以通过使用File方法从控制器操作返回FileResult来实现:

public ActionResult Download()
{
    string file = @"c:\someFolder\foo.xlsx";
    string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    return File(file, controntType, Path.GetFileName(file));
}

and then replace your button with an anchor pointing to this controller action:

然后用指向此控制器操作的锚点替换您的按钮:

@Html.ActionLink("Button 1", "Download", "SomeController")

Alternatively to using an anchor you could also use an html form:

或者使用锚点,您也可以使用html表单:

@using (Html.BeginForm("Download", "SomeController", FormMethod.Post))
{
    <button type="submit">Button 1</button>
}

If the file is located inside some non-accessible from the client folder of your application such as App_Data you could use the MapPath method to construct the full physical path to this file using a relative path:

如果文件位于应用程序的客户端文件夹(例如App_Data)中的某些不可访问的文件中,则可以使用MapPath方法使用相对路径构造此文件的完整物理路径:

string file = HostingEnvironment.MapPath("~/App_Data/foo.xlsx");

#2


2  

HTML:

<div>@Html.ActionLink("UI Text", "function_name", "Contoller_name", new { parameterName = parameter_value },null) </div>

Controller:

public FileResult download(string filename) {
        string path = "";
        var content_type = "";
        path = Path.Combine("D:\file1", filename);

        if (filename.Contains(".pdf"))
        {
            content_type = "application/pdf";
        }
        return File(path, content_type, filename);
}