asp教程.net c#(htmlinputfile)上傳圖片代碼
先看一下htmlinputfile檔案上傳功能
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( "上傳照片失敗! ");
throw;
}
}
}
下面一款應用執行個體
%@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 = "請選擇要上傳的檔案!";
}
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.executenonquery();
conn.close();
txtdesc.text = "";
txtmess.text = "ok!你已經成功上傳了類型的檔案";
}
}
catch(exception ex)
{
txtmess.text = ex.message.tostring();
}
}
</script>
<html>
<head>
<title>上傳圖片</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>上傳圖片</td>
<td>
<input type="file" id="up_file" runat="server" style="width:320" accept="text/*" name="up_file">
</td>
</tr>
<tr>
<td>檔案說明</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="上傳" />
</td>
</tr>
</table>
</form>
</body>
</html>
//顯示圖片
<%@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教程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.executereader();
read.read();
response.contenttype = (string)read["type"];
response.outputstream.write((byte[])read["imgdata"], 0, (int)read["imgsize"]);
response.end();
conn.close();
}
</script>