如何下載非靜態的pdf文件。C#MVC下載動態文件名
我嘗試這樣做:
public ActionResult GetPdf(string filename)
{
var invoice = db.Invoices.ToList().Find(x=>x.InvoicePath==filename);
return File("~/Invoice/" +invoice.ToString(), "application/pdf",Server.UrlEncode(invoice.ToString()));
}
索引視圖
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.CreatedInvoice)
</th>
<th>
@Html.DisplayNameFor(model => model.InvoicePath)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CreatedInvoice)
</td>
<td>
@Html.DisplayFor(modelItem => item.InvoicePath)
</td>
<td>
@Html.ActionLink("Download", "GetPdf", new { id = item.InvoicePath })
</td>
</tr>
}
</table>
這是我上傳文件的方法
public ActionResult fileupload(HttpPostedFileBase file)
{
if (file != null)
{
string ImageN = System.IO.Path.GetFileName(file.FileName);//get the file path
string physicalPath = Server.MapPath("~/Invoice/" + ImageN);//store the file path in a folder called img
file.SaveAs(physicalPath);
Invoice newP = new Invoice();
newP.InvoicePath = ImageN;
newP.CreatedInvoice = DateTime.Now;
db.Invoices.Add(newP);//store the image path in a database
db.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
任何建議,將不勝感激
您使用發票.ToString()創建路徑 - 你是否重寫Invoice對象的ToString()方法返回路徑?你能看到正在生成的路徑嗎? –