File Being Download As Byte[] Format-VBForums
Results 1 to 4 of 4

Thread: File Being Download As Byte[] Format

  1. #1

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

    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
    Hyperactive Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    389

    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
    Member FunkySloth's Avatar
    Join Date
    Aug 2016
    Posts
    37

    Re: File Being Download As Byte[] Format

    When adding the third parameter, causing "ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION".

  4. #4
    Hyperactive Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    389

    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?

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.