简体   繁体   English

从物理路径下载ASP mvc 4中的文件

[英]DownloadFile in ASP mvc 4 from physical path

I need to make downloading files from the server .我需要从服务器下载文件。 Server is specified physical path.服务器是指定的物理路径。 the file is downloaded , but there are problems that I do not know how to fix .文件已下载,但有一些我不知道如何解决的问题。 1) All the files are named as "Download".2) does not have an extension . 1) 所有文件都命名为“下载”。2) 没有扩展名。 Help me, please, I'm new to ASP MVC请帮助我,我是 ASP MVC 的新手

Code of Controller :控制器代码:

        public ActionResult NIRInfo()
    {
        List<string> filesList = new List<string>();
        var dir = new System.IO.DirectoryInfo(@"Z:\Docs");
        System.IO.FileInfo[] fileNames = dir.GetFiles("*.*");
        var xow = from i in db.NIRs where i.Id == id select i.File;
        foreach (var i in xow)
        {
            fileNames = dir.GetFiles(i);
            foreach (var f in fileNames)
            {
                filesList.Add(f.Name);
            }
            ViewData["fList"] = filesList;
        }
        return View(nir);

    }

    public FileResult Download(string Name)
    {
        return File(@"Z:\Docs\" + Name,  MediaTypeNames.Application.Octet);
    }

Code of view视图代码

                            @{ var fList = ViewData["fList"] as List<string>;}

                        @if (ViewData["fList"] != null)
                        {
                            <table>
                                @foreach (var f in fList)
                                {
                                    <tr>
                                        <td>
                                            <a href="@Url.Action("Download", "NIR", new {  Name = @f })">@f</a>
                                        </td>
                                    </tr>
                                }
                            </table>
                        }

You need to specify a filename in method overload.您需要在方法重载中指定文件名。 Look at third parameter in File method查看File方法中的第三个参数

public FileResult Download(string Name)
{
    return File(@"Z:\Docs\" + Name,  MediaTypeNames.Application.Octet, "document.txt");
}
HttpResponseBase response;

response.Clear();
response.AddHeader("content-disposition", "attachment;filename=\"" + fileName + "\"");
response.TransmitFile(Path.Combine(FileName, Path.GetFileName(fileName )));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.net mvc 3 从物理路径加载 razor 视图 - Asp.net mvc 3 load razor views from a physical path ASP物理和虚拟路径 - ASP physical and virtual path ASP,NET MVC在上载文件时将虚拟路径更改为物理路径 - ASP,NET MVC change a virtual path to a physical path when uploading a file 路由需要忽略名为“ ClientBin”的文件夹中的内容,该文件夹是服务器上的绝对物理路径,因此不会被路由ASP.NET MVC - Routing Needs to Ignore content from a folder called “ClientBin” which is absolute Physical path on Server so that it Won't get routed ASP.NET MVC 谁能告诉我如何将物理文件保存到配置提供的路径 - ASP.NET Core MVC - Can Anyone Show Me How to Save Physical Files to a Path Provided by Configuration - ASP.NET Core MVC ASP.NET MVC5拒绝映射与物理路径匹配的路由 - ASP.NET MVC5 refuses to map a route which matches a physical path mvc web api中文件的真实物理路径 - Real physical path of file in mvc web api 将路由绑定到MVC4中的自定义物理路径 - Bind route to custom physical path in mvc4 将MVC视图路由值映射到物理路径 - Map MVC View Route Values to physical path ASP.NET实体模型物理路径 - ASP.NET Entity Model Physical Path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM