什么是控制器代码。如何设置服务器文件夹的路径。
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();
这是我在文件夹中下载文件的方式,它有效。
您可以使用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" })