I would like to upload an image from an asp.NET Web Forms (4.0) application to an Oracle 11g database. Eventually, I would also need to retrieve the image.
I was told to use a blob type, but this can change if a better solution is found.
From the front end perspective I am using the asp.NET File Upload component and is great to select the path of the file.
What is the best way around it? Work with blob's? work with bfile's? upload the image on the server and just store the path?
Assuming you have a table called IMAGES with a BLOB column, you'll want to do something similar to:
byte[] imageData = FileUpload1.FileBytes;
OracleCommand cmd = new OracleCommand("INSERT INTO IMAGES(PARENT_ID, IMAGE_DATA) VALUES(:1, :2)", connection);
cmd.Parameters.Add("1", OracleDbType.Int32, parentID, ParameterDirection.Input);
cmd.Parameters.Add("2", OracleDbType.Blob, imageData, ParameterDirection.Input);
cmd.ExecuteNonQuery();
Update : When it comes to display the image, you mainly need to understand that the HTML page only contains a reference to the image ( <img src="ImageFromDatabase.ashx?id=1234">
) and that the image itself is served by a separate request. The following example uses a Generic Handler :
public void ProcessRequest (HttpContext context) {
HttpRequest request = context.Request;
int parentID = Int32.Parse(request.QueryString["id"]);
OracleCommand cmd = new OracleCommand("SELECT * FROM IMAGES WHERE PARENT_ID = :1", connection);
cmd.Parameters.Add("1", OracleDbType.Int32, parentID, ParameterDirection.Input);
OracleDataReader reader = cmd.ExecuteReader();
byte[] imageData = ((OracleBlob)reader["IMAGE_DATA"]).Value;
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(imageByte);
}
I've omitted all the error handling which you should be added in a real application.
Update 2:
If you have a grid view, you could define the image column like this:
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "ImageFromDatabase.ashx?id=" + Eval("ImageID")%>'/>
Best would be to upload the image on the server and store the path.
Why you ask?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.