Downloading a file from SQL to display on Blackberry 8300. RRS feed

  • Question

  • User-2142816325 posted

    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 application.

     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.


    1      private static void OpenFile(Guid AttachmentID)
    2            {
    3                const int CHUNK_SIZE = 1024 * 8; //8k. 
    5                SqlConnection conn = new SqlConnection(Helper.ConnectionString);
    6                SqlCommand cmd = new SqlCommand("usp_GetBinaryFile", conn);
    7                cmd.CommandType = CommandType.StoredProcedure;
    8                cmd.Parameters.Add(SqlCommandHelper.CreateParameter("@AttachmentID", SqlDbType.UniqueIdentifier, AttachmentID));
    10               try
    11               {
    12                   conn.Open();
    13                   using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
    14                   {
    15                       reader.Read();
    16                       //Get the file size by passing null as the byte array parameter.
    17                       long fileSize = reader.GetBytes(0, 0, null, 0, 0);
    19                       //Set up the file for streaming to user.
    20                       HttpContext.Current.Response.Clear();
    21                       HttpContext.Current.Response.ClearContent();
    22                       HttpContext.Current.Response.ClearHeaders();
    23                       //I've changed this bit of code just to shorten the process, this is for downloading
    24                       //A known pdf file.
    25                       HttpContext.Current.Response.ContentType = "application/pdf";
    26                       HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
    27                       HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + "MyFileName.pdf");
    29                       long bytesRead = 0;
    30                       byte[] buffer = new byte[CHUNK_SIZE];
    31                       int read = 0;
    32                       while (bytesRead < fileSize)
    33                       {
    34                           if (HttpContext.Current.Response.IsClientConnected)
    35                           {
    36                               read = (int)reader.GetBytes(0, bytesRead, buffer, 0, buffer.Length);
    37                               HttpContext.Current.Response.OutputStream.Write(buffer, 0, read);
    38                               HttpContext.Current.Response.Flush();
    39                               bytesRead += read;
    40                           }
    41                           else
    42                           {
    43                               bytesRead = fileSize;
    44                           }
    45                       }
    46                   }
    47               }
    48                  catch(Exception ex)
    49               {
    50                   throw new ApplicationException("There has been an error trying to open the file.", ex);
    51               }
    52               finally
    53               {
    54                   if (conn.State != ConnectionState.Closed)
    55                       conn.Close();
    56                   HttpContext.Current.Response.Flush();
    57               }


    Any ideas? If I find the answer I will post it.

    Thursday, June 19, 2008 7:26 AM

All replies

  • User1634317999 posted


    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.

    Friday, June 20, 2008 1:55 AM
  • User-2142816325 posted


     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.

    case "doc":
    case "rtf":

    ctype = "application/msword";


    case "csv":

    case "xls":

    ctype = "application/vnd.ms-excel";


    case "mdb":

    ctype = "application/vnd.ms-access";



    Friday, June 20, 2008 4:38 AM