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 ";
}
}
}