【问题标题】:How can I download a PDF file from a path or folder using asp.net如何使用 asp.net 从路径或文件夹下载 PDF 文件
【发布时间】:2020-11-17 07:15:38
【问题描述】:

下午好,我有一个 asp.net 项目,我希望通过一个 asp.net c# 表单,患者可以通过他们的识别号或代码从路径或文件夹下载证书,该文件夹包含 pdf并按ID号组织

谢谢

【问题讨论】:

  • 您的问题过于宽泛,不符合社区准则。请写下您面临的问题,到目前为止您尝试过的内容,分享您的一些代码。同时更正标签。是关于 asp.net 还是 asp.net.mvc 的问题 - 两种截然不同的技术。

标签: asp.net asp.net-mvc asp.net-core asp.net-mvc-4 asp.net-mvc-3


【解决方案1】:

由于你的描述模糊,我不确定你是不是一个mvc项目 或核心项目。

以下是各项目下载pdf的案例,请参考:

在mvc中:

        public ActionResult DownLoad()
        {
            return View();
        }

        [HttpPost]
        public ActionResult DownLoad(string id)
        {
            //PdfFiles is the name of the folder where these pdf files are located
            var path = Server.MapPath("~/PdfFiles/pdf" + id + ".pdf");  
            var memory = new MemoryStream();
            using (var stream = new FileStream(path, FileMode.Open))
            {
                stream.CopyToAsync(memory);
            }
            memory.Position = 0;
            return File(memory, "application/pdf", Path.GetFileName(path)); 
        }

查看:

<form  method="post" action="DownLoad">
    Pdf Id: <input id="Text1" type="text" name="id" />
    <input id="Submit1" type="submit" value="submit" />
</form>

这是测试结果:

在核心中:

       public IActionResult DownLoad()
        {
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> DownLoad(string id)
        {
           //here i put the PdfFiles folder in the wwwroot folder
            var path = Path.Combine(
                           Directory.GetCurrentDirectory(),
                           "wwwroot", "PdfFiles/pdf" + id + ".pdf");

            var memory = new MemoryStream();
            using (var stream = new FileStream(path, FileMode.Open))
            {
                await stream.CopyToAsync(memory);
            }
            memory.Position = 0;
            return File(memory, "application/pdf", Path.GetFileName(path));
        }

查看:

<form asp-action="DownLoad" method="post">
     Pdf Id: <input id="Text1" type="text"  name="id"/>
    <input id="Submit1" type="submit" value="submit" />
</form>

这是测试结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2010-10-14
    • 2020-08-18
    • 1970-01-01
    • 2018-11-09
    相关资源
    最近更新 更多