Asp.net code for uploading images to the database

Source: Internet
Author: User

Database: stores the binary data storage fields of images.
Front-end: CopyCode The Code is as follows: <% @ page Language = "C #" autoeventwireup = "true" codebehind = "uploadwork. aspx. cs" inherits = "meishuguan. uploadwork" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Style type = "text/CSS">
. Style1
{
Width: 100%;
}
. Style2
{
Height: 25px;
}
</Style>
</Head>
<Body>
<SCRIPT type = "text/JavaScript">
Function checkdata (){
VaR filename = Document. getelementbyid ("uploadimage"). value;
Alert (filename );
// Var filename = Document. getelementsbyname ("uploadimage"). value;
If (filename = "")
Return;
VaR exname = filename. substr (filename. lastindexof (".") + 1). touppercase ();
// Alert (exname)
If (exname = "jpg" | exname = "BMP" | exname = "GIF "){
VaR imgpath = filename. SRC;
Alert (imgpath );
Document. getelementbyid ("previewimage"). src = imgpath;
Document. Write (filename );
}
Else {
Alert ("select the correct image file ")
Document. getelementbyid ("previewimage"). value = ""
}
}
</SCRIPT>
<Form method = "Post" runat = "server">
<Div>
<Table class = "style1">
<Tr>
<TD class = "style2">
<Asp: Label id = "messagelabel" runat = "server"> </ASP: Label>
</TD>
<TD class = "style2">

</TD>
</Tr>
<Tr>
<TD class = "style2">
<Input id = "uploadimage" name = "uploadimage" type = "file" runat = "server" onchange = "checkdata ()"/>
</TD>
<TD class = "style2">

</TD>
</Tr>
<Tr>
<TD>
<Asp: button id = "uploadbutton" runat = "server" text = "OK" onclick = "uploadbutton_click"/>
</TD>
<TD>

</TD>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>

Background: Copy code The Code is as follows: using system;
Using system. Collections. Generic;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. IO;
Using system. Data. sqlclient;
Using system. configuration;
Namespace meishuguan
{
Public partial class uploadwork: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void uploadbutton_click (Object sender, eventargs E)
{
Httppostedfile upfile = uploadimage. postedfile;
Int imagelength = upfile. contentlength;
If (imagelength = 0)
{
Messagelabel. Text = "select the image to upload ";
Return;
}
If (imagelength> int32.parse (application ["maximagelength"]. tostring ()))
{
Messagelabel. Text = "the image size cannot exceed 2 MB ";
Return;
}
Stream imagestream = upfile. inputstream;
Byte [] imagebyte = new byte [imagelength];
Imagestream. Read (imagebyte, 0, imagelength );
Sqlconnection connection = new sqlconnection (configurationmanager. connectionstrings ["connectionstring"]. tostring ());
String sqlstring = "insert into [work] (MID, image, length) values (@ mid, @ image, @ length )";
Sqlcommand command = new sqlcommand (sqlstring, connection );
Command. Parameters. Add ("@ mid", system. Data. sqldbtype. INT). value = session ["mid"]. tostring ();
Command. Parameters. Add ("@ image", system. Data. sqldbtype. Image, imagelength). value = imagebyte;
Command. Parameters. Add ("@ length", system. Data. sqldbtype. INT). value = imagelength;
Connection. open ();
Command. executenonquery ();
Connection. Close ();
Messagelabel. Text = "image uploaded successfully ";
}
}
}

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.