Comment puis-je télécharger un fichier PDF qui n'est pas statique.C# MVC Télécharger le nom de fichier dynamique
J'ai essayé ceci:
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()));
}
L'indice Voir
<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>
Ceci est ma méthode de fichier de téléchargement
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();
}
Toutes les suggestions seront très appréciés
Vous utilisez la facture .ToString() pour créer le chemin - avez-vous remplacé la méthode ToString() dans l'objet Invoice? Pouvez-vous voir quel chemin est généré? –