-
Jun 7th, 2017, 11:02 PM
#1
Thread Starter
Lively Member
[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
-
Jun 8th, 2017, 12:25 PM
#2
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");
-
Jun 12th, 2017, 09:10 PM
#3
Thread Starter
Lively Member
Re: File Being Download As Byte[] Format
When adding the third parameter, causing "ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION".
-
Jun 13th, 2017, 07:06 AM
#4
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?
-
Jun 29th, 2017, 08:02 PM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|