0

I want to use the opening Pdf file, and the User inputs some of the fields and then saves this file into the Specific folder in my Asp.net folder.

I try to use the HttpPostedFileBase file but I cannot save this file to a folder, always become null for Parameter. Is it working right? You guys have any other idea for open Pdf and edit, save as pdf file to a folder?

<form method="post" enctype="multipart/form-data">

<div>

     <embed name="file" src="~/AnnounceFile/PDF.pdf" 
     style="width: 780px; height:980px;"                            
     class="with-200" />

    <button type="submit">Import</button>

</div>

</form>

Controller

 [HttpPost]

    public ActionResult Index(HttpPostedFileBase file)

    {

        string filename = Guid.NewGuid() + Path.GetExtension(file.FileName);

        string filepath = "/folder/" + filename;

        file.SaveAs(Path.Combine(Server.MapPath("/excelfolder"), filename));

        InsertExceldata(filepath, filename);

        return View(db.Iteminfoes.ToList());
  }
Mehran Gharzi
  • 136
  • 1
  • 10
sagn
  • 1
  • 2

1 Answers1

0

You can open the pdf using target="_blank" in a href tag File Name

The PDFHelper.GeneratePDF is returning array of bytes of PDF file. As I understood, after that you need to store this PDF in local folder. In that case you can use.

protected void save_pdf()
{
   String path_name = "~/PDF/";
   var pdfPath = Path.Combine(Server.MapPath(path_name));
   var formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);

   string username = "Test";
   string password = "12345";      
   String file_name_pdf = "Test.pdf";

   var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);
   File.WriteAllBytes(Path.Combine(pdfPath, file_name_pdf), pdfContents);

   WebRequest request = WebRequest.Create(Server.MapPath("~/PDF/" + pdfContents));
   request.Method = WebRequestMethods.Ftp.UploadFile;

   request.Credentials = new NetworkCredential(username, password);
   Stream reqStream = request.GetRequestStream();      
   reqStream.Close();
}

Also, please see Can a Byte[] Array be written to a file in C#?