Last post Jun 20, 2008 04:38 AM by pmatthews05
Jun 19, 2008 07:26 AM|pmatthews05|LINK
I'm hoping someone can help me. I've written an application that allows a user to upload a file which is in turn stored within SQL 2005. I have also written a web page that when the user clicks on a filename the code obtains the file from SQL and then renders
it so that use can either view or download the file.
I have then written a page that is for mobile users, mainly the Blackberry 8300 as that is the company default mobile device. My problem is when this device trys to display the file, it displays it as random characters which I believe is the Binary. (Like
trying to open a word document in notepad).
The Blackberry does allow to view word/excel/pdf documents, because if I create a webpage and put a link directly on the page that links to file that's sitting on the webserver not in SQL it opens the file fine. Unforunately that's not an option for our
I originally tried to use the ContentType as "application/octet-stream" but the Blackberry didn't like that, so I have now been using the correct Content Type for the correct file type. I've shorten my code below to just download pdf only.
I've included my code below, does anyone know how I need to change this so that the Blackberry will open as a pdf not as binary text.
private static void OpenFile(Guid AttachmentID)
const int CHUNK_SIZE = 1024 * 8; //8k.
SqlConnection conn = new SqlConnection(Helper.ConnectionString);
SqlCommand cmd = new SqlCommand("usp_GetBinaryFile", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(SqlCommandHelper.CreateParameter("@AttachmentID", SqlDbType.UniqueIdentifier, AttachmentID));
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
//Get the file size by passing null as the byte array parameter.
long fileSize = reader.GetBytes(0, 0, null, 0, 0);
//Set up the file for streaming to user.
//I've changed this bit of code just to shorten the process, this is for downloading
//A known pdf file.
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + "MyFileName.pdf");
long bytesRead = 0;
byte buffer = new byte[CHUNK_SIZE];
int read = 0;
while (bytesRead < fileSize)
read = (int)reader.GetBytes(0, bytesRead, buffer, 0, buffer.Length);
HttpContext.Current.Response.OutputStream.Write(buffer, 0, read);
bytesRead += read;
bytesRead = fileSize;
throw new ApplicationException("There has been an error trying to open the file.", ex);
if (conn.State != ConnectionState.Closed)
Any ideas? If I find the answer I will post it.
Jun 20, 2008 01:55 AM|SKT_01|LINK
a first step to solve your problem could be to check if the download works in a web browser like IE. If this is the case it is a BlackBerry issue and you perhaps get the answer in the
BlackBerry Browser Discussion Forum.
Jun 20, 2008 04:38 AM|pmatthews05|LINK
Thank you for your reply but I think I've solved my own problem now. it appears I was using the content type wrong, (well I think that's what it was). When I re-coded the code to paste into here, I hand wrote
contentType = "application/pdf"
where before I had it as "Application/pdf" and although Windows Broswer didn't seem to be case sensitive the Blackberry browser was.
My other working content types for anyone else are, I'm still looking to ensure the txt, jpg, png, tiff and bmp work but they aren't as important to the application as pdf, doc, xls.