Results 1 to 5 of 5

Thread: [RESOLVED] File Being Download As Byte[] Format

  1. #1

    Thread Starter
    Lively Member FunkySloth's Avatar
    Join Date
    Aug 2016
    Posts
    91

    Resolved [RESOLVED] File Being Download As Byte[] Format

    Hi

    Code:
            public ActionResult DownloadFile()
            {
                try
                {
                    SqlCommand command = new SqlCommand();
                    DataTable returnTable = new DataTable();
    
                    using (SqlConnection DBConnection = new SqlConnection(DatabaseConnection.DepoConnection))
                    {
                        command = new SqlCommand("GetFile", DBConnection);
                        command.CommandType = CommandType.StoredProcedure;
    
                        command.Parameters.AddWithValue("@CODE", HttpContext.Session["CODE"].ToString());
    
                        DBConnection.Open();
                        returnTable.Load(command.ExecuteReader());
                    }
    
                    byte[] content = { };
                    content = (byte[])returnTable.Rows[0]["FILE"];
                    Response.AppendHeader("Content-Disposition", "inline; filename=" + content);
                    return File(content, "application/pdf");
                }
                catch (Exception ex)
                {
                    HttpContext.Session["ErrorMessage"] = ex.Message;
                    return RedirectToAction("Index", "ServerError");
                }
            }
    Above code is getting the PDF files from database and will open it in the browser. However, when I'm going to download the file using the built-in download function of PDF reader in browser, it download as Byte[] Format, to be able to correct the file, I must put the .pdf prefix before saving it.

    How to solve this issue

    Thank you

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: File Being Download As Byte[] Format

    You could give it a filename when you return it e.g.
    Code:
    return File(content, "application/pdf", "filename.pdf");

  3. #3

    Thread Starter
    Lively Member FunkySloth's Avatar
    Join Date
    Aug 2016
    Posts
    91

    Re: File Being Download As Byte[] Format

    When adding the third parameter, causing "ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION".

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: File Being Download As Byte[] Format

    You probably don't need the
    Code:
    Response.AppendHeader("Content-Disposition", "inline; filename=" + content);
    bit if you are supplying a filename.

    I have done similar code before and never had the browser do anything other than display the pdf in the browser, what browser are you using?

  5. #5

    Thread Starter
    Lively Member FunkySloth's Avatar
    Join Date
    Aug 2016
    Posts
    91

    Re: File Being Download As Byte[] Format

    I created two separate function instead (download and view) since it keep saying I declared Content-Disposition twice.

    Thank you for the help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width