Image displays images under any absolute path on the server. of is implemented as a binary stream

  • 2020-05-24 05:31:19
  • OfStack

I want to implement the following requirements: the database is stored in the absolute path of the photos (can not be under the path of the system), Image control dynamic loading of the photos under the path.

And when you look at this problem, some people say, well, that's not easy, let's just set URL to an absolute path. I can only say that if you say so, just means you haven't thought of even, do not yet understand Web developing foreground code and code exactly is what meaning, but this kind of practice, when it yourself (not to IIS), sogou browser can display images (that's 1 can be displayed, so it doesn't make sense).

Image controls is in System Web. UI. WebControls named control, so can't through byte like in winform [] directly display images (individual also a bit don't understand this sentence, if anyone understand can explain 1 below). The method found is to read the image, then write the incoming binary stream to a page, and then set Image's URL to this page. Here is my Demo implementation code.
Foreground code:
 
<head runat="server"> 
<title></title> 
<script src="jquery-1.7.1.js" type="text/javascript"></script> 
<script type="text/javascript"> 
function ShowP(obj) { 
$("#Image1")[0].src = "ShowPic.aspx?URL=" + obj.id; 
} 
</script> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
<input id="E:\\1.jpg" type="button" value="button" onclick="ShowP(this);" /> 
<asp:Image ID="Image1" runat="server" /> 
</div> 
</form> 
</body> 
</html> 

The foreground code of ShowPic.aspx is empty. The background code of ShowPic.aspx is as follows:
 
public partial class ShowPic : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
#region " Depending on the photo path, convert the photo to 2 Hexadecimal array " 
string strUrl = Request.QueryString["URL"]; 
//  In order to 2 Read the file in base mode  
FileStream aFile = new FileStream(strUrl, FileMode.OpenOrCreate, FileAccess.ReadWrite); 
//  create 1 a 2 Base data stream reader, associated with open files  
BinaryReader brMyfile = new BinaryReader(aFile); 
//  Reposition the file pointer to the start of the file  
brMyfile.BaseStream.Seek(0, SeekOrigin.Begin); 
// Gets an array of bytes for the photo  
byte[] photo = brMyfile.ReadBytes(Convert.ToInt32(aFile.Length.ToString())); 
//  Shut down more than new Each object of  
brMyfile.Close(); 
#endregion 
Response.BinaryWrite(photo); 
} 
} 

Above, I assigned the path of the photo to the button ID, and then used the path as a parameter to call ShowPic.aspx. In the background code of ShowPic, the photo is written to the page in the form of a 2-dimensional array.

Then set Url for Image to this page, and the image will be displayed. I realized the dynamic loading of picture 1 through the button ID, and then the specific application. There is also a little bit of a small problem of JavaScript, about JavaScript, we still need to do an example by ourselves, through the javascript debugging tool, to understand the dom structure, and then operate.

Related articles: