The picture is stored in binary form in the database and read out from the database to show on the page __ database

Source: Internet
Author: User

A recent. NET project, I want to implement the picture in binary form to save in the database, and read out from the database displayed on the page. I've never done anything like that before. After consulting a large number of data, will realize the idea of first come down for your reference.

1 First upload the file. Using the controls in HTML enables you to upload photos.

2 Converts the client address that the control obtains into an address that can read binary data. This step is the key, need to use System.IO.getpath to upload the path of photos to the path of transformation. The FileStream control is then used to read the photo to the server on the transferred address. Note the server needs to set write permissions.

3. Use the FileStream to read the photo written to the specified path and save it to the database in binary form.

4. When reading data from a database, you need to first read the binary data to a specified page B using the Response.BinaryWrite method.

5. Specify the ImageUrl address of the Image control on page A that will display the picture as the network relative address of page B. Note I give Mageurl the best assignment in the background, do not write dead in the foreground.

< HTML >
< head >
< title > Multi-File upload </title >
< script language = "JavaScript" > ...
function AddFile ()
... {
var str = ' <input type= "file" size= "name=" file ">"
document.getElementById (' MyFile '). insertAdjacentHTML ("BeforeEnd", str)
}
</script >
< BODY >
< form ID = "Form1" method = "post" runat = "server" enctype = "Multipart/form-data" >
< Center >
< Asp:label Runat = "server" ID = "MyTitle" ></Asp:label >
< p ID = "MyFile" >< INPUT type = "file" size = "NAME =" file "></P >
< P >
< input type = "button" value = "Increase (add)" onclick = "AddFile ()" >
< Asp:button Runat = "server" Text = "upload" ID = "Upload" ></Asp:button >
< input onclick = "this.form.reset ()" type = "button" VALUE = Reset (reset) >
</P >
</Center >
< P align = "center" >
< Asp:label ID = "strstatus" runat = "server" font-names = "Song body" font-bold = "True" font-size = "9pt" Width = "513px"
BorderStyle = "None" bordercolor = "white" ></Asp:label >
</P >
</form >
</Body >
</HTML >

Namespace Updownsfiles
... {
/**////<summary>
Summary description of the WebForm1.
</summary>
public class WebForm1:System.Web.UI.Page
... {
protected System.Web.UI.WebControls.Label MyTitle;
protected System.Web.UI.WebControls.Button Upload;
protected System.Web.UI.WebControls.Label strstatus;

private void Page_Load (object sender, System.EventArgs e)
... {
Place user code here to initialize page
Mytitle.text = "Upload.text = "Start uploading";
if (this. IsPostBack)
This. SaveFiles ();
}
Private Boolean SaveFiles ()
... {
Get file Form elements
httpfilecollection files = HttpContext.Current.Request.Files;
System.Text.StringBuilder strmsg=new System.Text.StringBuilder ("uploaded files are: Try
... {
for (int intcount= 0; intcount<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.