我想在mvc的服务器路径下载我的Pdf文件,请任何人指导我...使用代码

问题描述 投票:0回答:1

什么是控制器代码。如何设置服务器文件夹的路径。

string path = HttpContext.Server.MapPath("~/Areas/CreatePaperSet/PdfPaperSet");
HttpContext.Response.TransmitFile(path);


WebClient client = new WebClient();
byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
    Response.Clear();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=DownloadPaperSet.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.BinaryWrite(buffer);
    Response.End();

}


pdfDoc.Close();
asp.net-mvc pdf model-view-controller view
1个回答
1
投票

这是我在文件夹中下载文件的方式,它有效。

您可以使用fileName作为参数创建Action。在操作中,您将文件读取为byte []并返回File对象。

public ActionResult Download(string fileName)
 {
    string path = Server.MapPath("~/Content/PdfPaperSet");

    byte[] fileBytes = System.IO.File.ReadAllBytes(path + @"\" + fileName);

    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}

在cshtml文件中,将fileName =“yourfile.pdf”作为参数传递。

@Html.ActionLink("Download Your File", "Download", new { fileName = "yourfile.pdf" })
© www.soinside.com 2019 - 2024. All rights reserved.