2017-10-24 16 views
0

私はPDFファイルを作成する必要があります。このPdfファイルにはいくつかのデータが入っています。さて、主な要件は、私はPDFを作成した後、私はそれをダウンロードすることができるはずです。今、PDFが作成されていますが、ダウンロードできません。 そのMVCアプリケーション.. は、私は上記のコントローラは、ファイル名を取得したファイルを読み込み、PDFと ダウンロードを作成します作成後のPDFファイルのダウンロード

public class HoldFilesController : Controller 
{ 
    // GET: HoldFiles 
    string holdpath = ConfigurationManager.AppSettings["HoldPath"].ToString(); 
    public ActionResult Index() 
    { 
     DirectoryInfo dirInfo = new DirectoryInfo(holdpath); 

     List<FileInfo> files = dirInfo.GetFiles().ToList(); 

     return View("Index",files); 
    } 

    **The above controller gets filenames from a file directory** 

    public ActionResult ViewFile(string[] Name) 
    { 
     for (int i = 0; i < Name.Length; i++) 
     { 
      string filepath=holdpath + @"\" + Name[i]; 

      string result; 
      using (StreamReader streamReader = new StreamReader(filepath)) 
      { 
       result = streamReader.ReadToEnd(); 
      } 

      using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) 
      { 
       Document document = new Document(PageSize.A4, 10, 10, 10, 10); 

       PdfWriter writer = PdfWriter.GetInstance(document, memoryStream); 
       document.Open(); 

       Paragraph paragraph = new Paragraph(); 
       paragraph.Add(result); 
       document.Add(paragraph); 
       document.Close(); 
       byte[] bytes = memoryStream.ToArray(); 
       memoryStream.Close(); 
       Response.ContentType = "application/pdf"; 
       Response.AppendHeader("Content-Disposition", "attachment; 
filename=MyFile.pdf"); 
       Response.TransmitFile(Server.MapPath("~/Files/MyFile.pdf")); 
       Response.End(); 
       Response.Close(); 

      } 
     } 

さらに、参照用のコードを共有していますのでご注意ください。 コードをデバッグすると、その文書が作成されているのがわかります。しかし、それは

+0

'しかしit'のダウンロードに問題が発生したが - これを読んで、まさにトラブルあなたは – Equalsk

+0

を抱えている説明:https://stackoverflow.com/a/7088233/1662459 –

+0

作成されたpdfとレスポンスのバインディングは何ですか?あなたはファイルパスや応答のものを使用していませんが、何をすると思いますか? – romerotg

答えて

0

あなたはこのような何かを試すことができますダウンロードするに 悩みを持つ:

private FileResult ViewFile(string[] Name) 
     { 
     //create your file at filePath 
      byte[] fileBytes = System.IO.File.ReadAllBytes(filePath); 
      return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, "MyFile.pdf")); 
     } 
0

あなたがMyFileをダウンロードするには、ランダムなファイル名を使用しての代わりに、先ほど作成したファイルを使用しているようです。まずResponseの部分をusingの外に移動し、終了するときにusingブロックがそれを行うので、そこにはMemoryStreamの終了を停止します。だから、using外にあなたが何か書くべき:

// Create New instance of FileInfo class to get the properties of the file being downloaded 
FileInfo file = new FileInfo(filepath); 

// Checking if file exists 
if (file.Exists) 
{        
    // Clear the content of the response 
    Response.ClearContent(); 

    // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header 
    Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", file.Name)); 

    // Add the file size into the response header 
    Response.AddHeader("Content-Length", file.Length.ToString()); 

    // Set the ContentType 
    Response.ContentType = ReturnFiletype(file.Extension.ToLower()); 

    // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead) 
    Response.TransmitFile(file.FullName); 

    // End the response 
    Response.End(); 
} 
関連する問題