Asp tutorial. net c # (htmlinputfile) upload image code
Let's take a look at the htmlinputfile file upload function.
Httpfilecollection files = httpcontext. current. request. files;
If (files. count = 1)
{
Httppostedfile postedfile = files [0];
If (postedfile. filename! = "")
{
String filename = path. getextension (postedfile. filename );
String savepath = "/upload/" + filename;
Try
{
Postedfile. saveas (savepath );
}
Catch
{
This. showmessage ("failed to upload the photo! ");
Throw;
}
}
}
The following application example
% @ Page language = "c #" %>
<% @ Import namespace = "system. io" %>
<% @ Import namespace = "system. data" %>
<% @ Import namespace = "system. data. sqlclient" %>
<Script language = "c #" runat = "server">
Public void button_submit (object o, eventargs e)
{
Httppostedfile upfile = up_file.postedfile;
Int ifilelength = upfile. contentlength;
Try
{
If (ifilelength = 0)
{
Txtmess. text = "select the file to upload! ";
}
Else
{
Byte [] filebytearray = new byte [ifilelength];
Stream streamobject = upfile. inputstream;
Streamobject. read (filebytearray, 0, ifilelength );
Sqlconnection conn = new sqlconnection ("server = yy; uid = sa; pwd =; database = pany ");
String SQL = "insert into t_imgs (imgdata, type, description, imgsize) values"
+ "(@ Image, @ contenttype, @ imagedescription, @ imgsize )";
Sqlcommand cmd = new sqlcommand (SQL, conn );
Cmd. parameters. add ("@ image", sqldbtype. binary, ifilelength). value = filebytearray;
Cmd. parameters. add ("@ contenttype", sqldbtype. varchar, 50). value = upfile. contenttype;
Cmd. parameters. add ("@ imagedescription", sqldbtype. varchar, 200). value = txtdesc. text;
Cmd. parameters. add ("@ imgsize", sqldbtype. bigint, 8). value = upfile. contentlength;
Conn. open ();
Cmd.exe cutenonquery ();
Conn. close ();
Txtdesc. text = "";
Txtmess. text = "OK! You have successfully uploaded a file of the type ";
}
}
Catch (exception ex)
{
Txtmess. text = ex. message. tostring ();
}
}
</Script>
<Html>
<Head>
<Title> upload an image </title>
</Head>
<Body bgcolor = "# fffffa">
<Form enctype = "multipart/form-data" runat = "server" id = "form1">
<Table runat = "server" width = 700 align = left id = "table1" cellpadding = 0 cellspacing = 0 border = 0>
<Tr>
<Td> upload an image </td>
<Td>
<Input type = "file" id = "up_file" runat = "server" style = "width: 320" accept = "text/*" name = "up_file">
</Td>
</Tr>
<Tr>
<Td> File description </td>
<Td>
<Asp: textbox runat = "server" width = 230 id = "txtdesc" maintanstate = "false"/>
</Td>
</Tr>
<Tr>
<Td>
<Asp: label runat = "server" id = "txtmess" forecolor = red maintainstate = "false"/>
</Td>
<Td>
<Asp: button runat = "server" width = 230 text = "upload"/>
</Td>
</Tr>
</Table>
</Form>
</Body>
</Html>
// Display the image
<% @ Page language = "c #" %>
<% @ Import namespace = "system. data" %>
<% @ Import namespace = "system. data. sqlclient" %>
<Script language = "c #" runat = "server">
Public void page_load (object o, eventargs e)
{
Int imgid = convert. toint32 (request. params ["id"]);
String connstr = configurationsettings. apps tutorial ettings ["connectionstring"];
Sqlconnection conn = new sqlconnection (connstr );
String SQL = "select * from t_imgs where id = @ imgid ";
Sqlcommand cmd = new sqlcommand (SQL, conn );
Cmd. parameters. add ("@ imgid", sqldbtype.int). value = imgid;
Conn. open ();
Sqldatareader read = cmd.exe cutereader ();
Read. read ();
Response. contenttype = (string) read ["type"];
Response. outputstream. write (byte []) read ["imgdata"], 0, (int) read ["imgsize"]);
Response. end ();
Conn. close ();
}
</Script>