2017-05-10 1 views
0

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

+0

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é? –

Répondre

0

Je viens de modifier la méthode de téléchargement Pour ce

public ActionResult GetPdf(int id) 
     { 
      var invoice = db.Invoices.Find(id); 

      return File("~/Invoice/" + invoice.InvoicePath, "application/pdf", Server.UrlEncode(invoice.InvoicePath)); 
     } 

et changeais d'action Lien vers

@Html.ActionLink("Download", "GetPdf", new { id = item.InvoiceId }) 

ne sais vraiment pas pourquoi la méthode ci-dessus ne fonctionne pas

0

Cette i est ce que votre code devrait être:

public ActionResult GetPdf(string filename) 
{ 
    var invoice = db.Invoices.FirstOrDefault(x=> x.InvoicePath == filename); 
    return File("~/Invoice/" + invoice.InvoicePath, "application/pdf", Server.UrlEncode(invoice.InvoicePath)); 
} 

Je ne sais pas pourquoi vous Interrogation la base de données mais que vous obtenez les fileName dans paramater et qui est tout ce que vous devez servir le fichier de toute façon.

Vous enregistrez le nom de fichier dans la propriété InvoicePath lorsque le fichier est téléchargé, c'est cette propriété que vous devez utiliser pour obtenir le nom du fichier à télécharger.

+0

ok donc j'ai essayé comme vous l'avez posté mais j'obtiens l'erreur 0x80070002, URL demandée \t http: // localhost: 11809/Factures/GetPdf/Brief Increment Marquage rubric.pdf, chemin physique \t C: \ Users \ JephyDa1st \ Desktop \ Ds3Doc \ Ds3Project \ Supp \ Supp \ Factures \ GetPdf \ Brief Increment Marquage rubric.pdf –

+0

Votre méthode GetPdf est vraiment utilisée, n'est-ce pas? – sachin