asp.net上传图片保存到数据库的代码
程序员文章站
2024-03-08 12:26:04
数据库:保存图片的数据格式 图象二进制数据储存字段前台: 复制代码 代码如下: <%@ page language="c#" autoeventwireup="tru...
数据库:保存图片的数据格式 图象二进制数据储存字段
前台:
<%@ 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("请选择正确的图片文件")
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">
<img id="previewimage" alt="" src="" style="height: 80px; width: 80px" /></td>
</tr>
<tr>
<td>
<asp:button id="uploadbutton" runat="server" text="确定" onclick="uploadbutton_click" />
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台:
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 = "请选择要上传的图片";
return;
}
if (imagelength > int32.parse(application["maximagelength"].tostring()))
{
messagelabel.text = "图片大小不能大于2m";
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 = "图片上传成功";
}
}
}
前台:
复制代码 代码如下:
<%@ 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("请选择正确的图片文件")
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">
<img id="previewimage" alt="" src="" style="height: 80px; width: 80px" /></td>
</tr>
<tr>
<td>
<asp:button id="uploadbutton" runat="server" text="确定" onclick="uploadbutton_click" />
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台:
复制代码 代码如下:
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 = "请选择要上传的图片";
return;
}
if (imagelength > int32.parse(application["maximagelength"].tostring()))
{
messagelabel.text = "图片大小不能大于2m";
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 = "图片上传成功";
}
}
}
上一篇: java实现文件保存到本地的方法