Reputation: 93
I have database and image data example: 0xFFD8FFE000104A46494600010100000100010000FFE1018C45786966000049492A0008000000020031010200070000002600000069870400010000002E00000000000000476F6F676C6500000500009007000400000030323230099007000B0000007000000086920700080100007B00000002A00400010000006F02000003A0
and I think it is image converted to bytes. My Uploading code:
protected void Upload(object sender, EventArgs e)
{
FileUpload FileUpload1 = LoginView3.FindControl("FileUpload1") as FileUpload;
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
string email = User.Identity.Name;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "insert into tblFiles values (@Name, @ContentType, @Data, @email)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
cmd.Parameters.AddWithValue("@email", email);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
And now, I must convert data from database and display image in browser. I looking for tutorials and information about "image.fromstream", "Image handlers", "base64" and I don't know what I do wrong. I looking for ready method or tutorials or information about new I should have read. My sql database code:
CREATE TABLE [dbo].[tblFiles]
(
[id] INT IDENTITY (1, 1) NOT NULL,
[Name] VARCHAR (50) NOT NULL,
[ContentType] NVARCHAR (200) NOT NULL,
[Data] VARBINARY (MAX) NOT NULL,
[email] VARCHAR (50) NOT NULL
);
If I use "Image.fromstream", I will have this error " Error 1 'System.Web.UI.WebControls.Image' does not contain a definition for 'FromStream'" or "Error 1 A using namespace directive can only be applied to namespaces; 'System.Drawing.Image' is a type not a namespace"
I using this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.IO.Stream;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing.Image;
Upvotes: 0
Views: 260
Reputation: 62093
You can not send back an imagbe object. This is not how HTML works.
You ened to generate a URL for every image - that then gets embedded in the HTML where the image should be shown using the appropriate tag.
When the URL is then called, you return the proper metadata and the bytestream of the image from the database. You cando that via a handler / Controller action.
But you can not easily embed the image in the HTML (it is possible but realy bad practice for something like that) AND you can not return a System.Drawing or image or something - because the brwoser is not .NET
Upvotes: 1