locked
Serve database image with a handler page works, but save only as bmp RRS feed

  • Question

  • User1223969748 posted

    i use a handler page (image.ashx) to display images in database. it displays OK when i put src="image.ashx?id=123" into the image element. but when i right click the image and 'save picture as' it alway saves as 'untitled.bmp' in IE and obviously the size of file grows to size of a bitmap file. but it works well in firefox and save as jpg/gif/png with no problem. anyone have an idea how to make this work in IE as well? thanks.

    this is a sample of how i process the request in the handler:

     

    context.Response.Clear();
    context.Response.Expires = 0;
    context.Response.Cache.SetNoStore();
    context.Response.AppendHeader("Pragma", "no-cache");
    context.Response.ContentType = "image/png";
    context.Response.AddHeader("Content-Disposition", "inline;filename=imagename.png");
    context.Response.OutputStream.Write(bytes, 0, bytes.Length);
    context.Response.End();
     
    Wednesday, June 6, 2007 10:57 PM

Answers

  • User-900677162 posted

    HI, zeroxp:

    When you save an image in Microsoft Internet Explorer, the image is saved by default as a bitmap (.bmp). You do not have the option to save the image by using an extension other than .bmp. You experience this behavior even when the image uses a different format (such as .gif or .jpeg).

    For example, when you right-click an image on a Web page, and then click Save Picture As, the file name that appears in the File name box is Untitled, and the file type that appears in the Save as type box is Bitmap (*.bmp).

     

    <script type=text/javascript>loadTOCNode(1, 'cause');</script>
    This behavior may occur if a damaged program file (for example, an ActiveX or Java object) is downloaded to the <VAR>SystemRoot</VAR>\Downloaded Program Files folder on your hard disk.
     
    More information and solution:
    I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 10, 2007 11:08 PM

All replies

  • User-900677162 posted

    HI, zeroxp:

    When you save an image in Microsoft Internet Explorer, the image is saved by default as a bitmap (.bmp). You do not have the option to save the image by using an extension other than .bmp. You experience this behavior even when the image uses a different format (such as .gif or .jpeg).

    For example, when you right-click an image on a Web page, and then click Save Picture As, the file name that appears in the File name box is Untitled, and the file type that appears in the Save as type box is Bitmap (*.bmp).

     

    <script type=text/javascript>loadTOCNode(1, 'cause');</script>
    This behavior may occur if a damaged program file (for example, an ActiveX or Java object) is downloaded to the <VAR>SystemRoot</VAR>\Downloaded Program Files folder on your hard disk.
     
    More information and solution:
    I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 10, 2007 11:08 PM
  • User472117617 posted

    I think you need to change

    context.Response.AddHeader("Content-Disposition", "inline;filename=imagename.png");
    
     

    to this:

    context.Response.AddHeader("Content-Disposition", "attachment; filename=imagename.png"); 

    also,

    context.Response.AppendHeader("Pragma", "no-cache");
    kinda doesn't make sense for a file download [:)]

    Hope this helps,

    Matt

    Sunday, June 10, 2007 11:16 PM
  • User1089038237 posted

    Hi,

    Can any one help me please. [:'(] I need the same feature along with images need to display xls,txt,doc,gif and pdf files. I created a handler class named Handler.ashx. And the code as below

    <%@ WebHandler Language="C#" Class="Handler" %>
    using System;
    using System.Web;
    using System.Data;
    using System.Data.Sql;
    using System.IO;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
    using System.Data.Common;
    using System.Drawing;
    using System.Drawing.Imaging;
    public class Handler : IHttpHandler {
             
    private static bool ThumbnailCallback()
              {
                       
    return false;
              }
              publi
    c void ProcessRequest (HttpContext context)
              {
                        
    try
                        
    {
                                  
    string id = context.Request["id"];
                                  
    string filetype = context.Request["filetype"];
                                  
    if (id != null)
                                   {
                                           
    int PictureCol = 0;
                                          
    Database db = DatabaseFactory.CreateDatabase("Con");
                                          
    DbCommand cmd = db.GetStoredProcCommand("spS_SiteDoc_SiteDocId");
                                           db.AddInParameter(cmd,
    "@SiteDocId", DbType.Int64,id);
                                           db.AddInParameter(cmd,
    "@UserId", DbType.String, "");
                                          
    IDataReader dr = db.ExecuteReader(cmd);
                                           dr.Read();
                                          
    Byte[] file = new Byte[(dr.GetBytes(PictureCol, 0, null, 0, int.MaxValue))];
                                           
    string contentType = "";
                                          
    switch (filetype.ToLower())
                                           {
                                               
    case ".gif":
                                                     contentType =
    "image/gif";
                                                    
    break;
                                               
    case ".jpg":
                                                     contentType =
    "image/jpeg";
                                                    
    break;
                                                ca
    se ".doc":
                                                     contentType =
    "doc";
                                                     
    break;
                                               
    case ".txt":
                                                     contentType =
    "txt";
                                                    
    break;
                                               
    case ".pdf":
                                                     contentType =
    "pdf";
                                                    
    break
                                             }
                                             context.Response.ContentType = contentType;
                                             context.Response.OutputStream.Write(file, 0, file.Length);
                                             context.Response.Close();
                                  }
                   }
                   catch (Exception ex)
                   {
                           throw ne
    w Exception(ex.Message);
                   }
           }

     

           public bool IsReusable {
          
    get {
                
    return false;|
           }
       
    }
    }

    Apart from that I didn't do anything. Upon opening of Handler.ashx the program prompts for UserName & Password. Can any one help me please.I am not sure whether my approach is correct. So step by step instruction please.

     

     

     

    Monday, June 11, 2007 5:07 AM
  • User472117617 posted

    Filetype   MIME type

    .doc

     
    Monday, June 11, 2007 9:28 AM
  • User472117617 posted

    Filetype   MIME type

    .doc

    Monday, June 11, 2007 9:28 AM
  • User472117617 posted

    .doc = "application/msword"

    .pdf = "application/pdf"

    .txt = "text/plain"

     

    Monday, June 11, 2007 9:28 AM
  • User1089038237 posted

    Thanks a lot. Before I saw you message had solved the problem.
    I had to display files (gif,jpg,xls,txt,pdf,doc) in their own application.

    1. Created a Handler dll by opening Visual Studio C# and wrote the following code
      using System;
      using System.Data.SqlClient;
      using System.Collections.Generic;
      using System.Text;
      using System.Web;
      namespace MyHandler
      {
          public class HttpFileHandler : IHttpHandler
          {
              private static bool ThumbnailCallback()
              {
                  return false;
              }
              public void ProcessRequest(HttpContext context)
              {
                  try
                  {
                      string id = context.Request["id"];
                      string filetype = context.Request["filetype"];
                      //string UserId = context.Request["UserId"];
                      if (id != null)
                      {
                          SqlConnection con = new SqlConnection("Your connection");
                          SqlCommand cmd = new SqlCommand("SELECT varbinDoc from tblSiteDoc where intSiteDocId = " + id, con);
                          //SqlCommand cmd = new SqlCommand("spS_SiteDoc_SiteDocId", con);
                          //cmd.CommandType = System.Data.CommandType.StoredProcedure;
                          //cmd.Parameters.Add(new SqlParameter("@SiteDocId", System.Data.SqlDbType.Int, 999999999, id));
                          //cmd.Parameters.Add(new SqlParameter("UserId",System.Data.SqlDbType.VarChar,50));
                          string contentType = "";

                        switch (filetype.ToLower())
                        {
                            case "gif":
                                contentType = "image/gif";
                                break;
                            case "jpg":
                                contentType = "image/jpeg";
                                break;
                            case "doc":
                                contentType = "application/doc";
                                break;
                            case "txt":
                                contentType = "text/plain";
                                break;
                            case "pdf":
                                contentType = "application/pdf";
                                break;
                            case "xls":
                                contentType = "application/excel";
                                break;
                        }
                        con.Open();
                        SqlDataReader dr = cmd.ExecuteReader();
                        dr.Read();
                        context.Response.ContentType = contentType;
                        context.Response.BinaryWrite((Byte[])dr[0]);
                        con.Close();
                        context.Response.End();
                    }
                }
                catch (Exception ex)
                {

    •                 throw new Exception(ex.Message);
                  }
              }
    •         public bool IsReusable
              {
                  get
                  {
                      return false;
                  }
              }
          }
      }
    1. Compiled this into a dll.
    2. Added this dll to my web application
    3. Added the following HTTPHandler tags to my web config file of the application
            <add verb="*" path="Test.txt" type="MyHandler.HttpFileHandler, MyHandler" />
            <add verb="*" path="Test.doc" type="MyHandler.HttpFileHandler, MyHandler" />
            <add verb="*" path="Test.pdf" type="MyHandler.HttpFileHandler, MyHandler" />
            <add verb="*" path="Test.xls" type="MyHandler.HttpFileHandler, MyHandler" />
            <add verb="*" path="Test.image" type="MyHandler.HttpFileHandler, MyHandler" />
    4. IIS ---> Your virtual Directory ---> rightclick properties ----> Virtual Directory   -----------------> Configuration -------------> Add

    Browse for aspnet_isapi.dll, Extension .doc, Limit GET,POST,HEAD,DEBUG and untick verify that file exists. Follow the same for .xls,.image,.pdf,.doc.

    By doing so the HTTPHAndler handler opens the files with their own application

     




     

     

    Tuesday, June 12, 2007 8:03 AM