[英]unable to retrieve image from database?
我正在為此開發應用程序,我想捕獲圖像並將其存儲在數據庫ANA中,再次想要在網頁上檢索圖像,因為我嘗試了此代碼
context.Response.ContentType = "image/jpeg";
Stream strm = DisplayImage(theID);
byte[] buffer = new byte[2048];
int byteSeq = strm.Read(buffer, 0, 2048);
while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq=strm.Read(buffer, 0, 2048);
}
}
public Stream DisplayImage(int theID)
{
DB_Connection();
string sql = "SELECT Image_Data FROM Image_T WHERE Img_ID = '" + theID + "'";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("Img_ID", theID);
object imgg = (byte[])cmd.ExecuteScalar();
//byte[] imagedata = (byte[])cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])imgg);
}
catch
{
return null;
}
finally
{
con.Close();
}
}
在按鈕上點擊通話
protected void Button1_Click(object sender, EventArgs e)
{
Image1.ImageUrl = "Handler1.ashx?id=" + 101;
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ page import="com.itextpdf.text.pdf.PdfWriter"%>
<%@ page import="com.itextpdf.text.Document"%>
<%@ page import="com.itextpdf.text.DocumentException"%>
<%@ page import="com.itextpdf.text.Element"%>
<%@ page import="org.apache.pdfbox.pdmodel.PDDocument"%>
<%@ page import="org.apache.pdfbox.util.PDFTextStripper"%>
<%@ page import="org.apache.pdfbox.util.PDFTextStripperByArea"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.lang.*" %>
<%@ page import="com.itextpdf.*" %>
<jsp:useBean id="photo" class="api.images" scope="session" />
<body onload="setTimeout(function(){window.location = 'test.jsp';}, 1000)">
<%
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection con = DriverManager.getConnection(//my aws connection string//);
String temp=(String)session.getAttribute("myId");
String strSQL = "SELECT bImage FROM [Userdetails].[dbo].[upload_image] where iImageID='"+temp+"'";
嘗試{
int iNumPhoto ;
iNumPhoto=Integer.parseInt(temp);
con.setAutoCommit (false);
// get the image from the database
byte[] imgData = photo.getPhoto( con,iNumPhoto ) ;
// display the image
response.setContentType("image/gif");
OutputStream o = response.getOutputStream();
o.write(imgData);
o.flush();
o.close();
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
%>
試試上面的代碼。 將//我的連接字符串替換為您的///它應該具有遠程服務器連接,數據庫名稱,用戶名和密碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.