Displaying image from database… (copy from ASP.NET Forums)

來源:互聯網
上載者:User
    Running on ASP.NET 2.0 and SQL Server 2005. Have a table with an image-type column to store a photo. I have already created the script to upload/store the file in the DB, but I need code to handle the output.

I already have a stored proc call the retrieves the data based on an id... the recordset retrieved is one row with the photo and a few other text fields.

Please advise the necessary code to place in the codebehind file to display this photo. I am using <asp:label> tag for the positioning of the image.

Thanks!
-------------------------------------------
Re: Displaying image from database... Excellent... however, I am not storing the content type or file size in the DB... just the image file. I have this code below that is sending me the file, but there is no image... although the size of the file is the same as the original one uploaded. Any ideas?

System.Configuration.Configuration rootWebConfig =
            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/mysite");
        connString = rootWebConfig.ConnectionStrings.ConnectionStrings["strDataSource"];

        SqlConnection objConnection = new SqlConnection(connString.ConnectionString);
        SqlDataAdapter objCommand = new SqlDataAdapter("SelectProfile", objConnection);
        DataSet objDataSet = new DataSet();

        objConnection.Open();
        objCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
        objCommand.SelectCommand.Parameters.Add(new SqlParameter("@user_id", SqlDbType.Char, 50));
        objCommand.SelectCommand.Parameters["@user_id"].Value = Request.Params["id"];
        objCommand.Fill(objDataSet, "UserAccount");

byte[] fileData = (byte[])objDataSet.Tables["UserAccount"].Rows[0]["photo"];
        int fileSize = fileData.Length;

        Response.ClearContent();
        Response.ClearHeaders();
        Response.Clear();
        Response.Buffer = false;
        Response.ContentType = "image/JPEG";
        Response.AddHeader("Content-Disposition", "attachment; filename=MyPhoto.jpg");
        Response.AddHeader("Content-Length", fileSize.ToString());
        Response.BinaryWrite(fileData);

        objConnection.Close();
        Response.End();

Re: Displaying image from database...

here are some code bits I have used in the past to get binary data from sql server, but I only know how to response write the data as the full output for a page and then reference it in the html, I would try a searching on google for actually binding binary image data to the page (not sure if it can be done), I seroiusly doubt that you will be able to bind to an asp:label

here is how I have done this in the past
html in an aspx page where you want to show an image from the database
<img src="GetImage.aspx?id=39" />

then you create a GetImage.aspx page that responses gif or jpg data

// get data from dbfileTitle = rs["FileTitle"].ToString();fileSize = rs["FileSize"].ToString();contentType = rs["ContentType"].ToString();byte[] fileData = (byte[]) rs["FileData"];// response outputResponse.ClearContent();Response.ClearHeaders();Response.Clear();Response.Buffer = false;Response.ContentType = contentType;Response.AddHeader("Content-Disposition", "attachment; filename=" + fileTitle);Response.AddHeader("Content-Length", fileSize.ToString());Response.BinaryWrite(fileData);

------------------------------------

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.