asp.net上傳圖片保存到數(shù)據(jù)庫的代碼
更新時間:2010年07月22日 19:17:08 作者:
有時候某種需要將圖片保存到數(shù)據(jù)庫中,那么下面的代碼就可以參考下,下面沒有數(shù)據(jù)庫的建表說明,但數(shù)據(jù)庫需要建立下。
數(shù)據(jù)庫:保存圖片的數(shù)據(jù)格式 圖象二進(jìn)制數(shù)據(jù)儲存字段
前臺:
<%@ 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 = "圖片上傳成功";
}
}
}
前臺:
復(fù)制代碼 代碼如下:
<%@ 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>
后臺:
復(fù)制代碼 代碼如下:
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 = "圖片上傳成功";
}
}
}
您可能感興趣的文章:
- asp.net fileupload控件上傳圖片并預(yù)覽圖片
- ASP.net WebAPI 上傳圖片實例
- asp.net上傳圖片并作處理水印與縮略圖的實例代碼
- asp.net MVC實現(xiàn)無組件上傳圖片實例介紹
- asp.net+FCKeditor上傳圖片顯示叉叉圖片無法顯示的問題的解決方法
- ASP.NET下上傳圖片到數(shù)據(jù)庫,并且讀出圖片的代碼(詳細(xì)版)
- asp.net UpdatePanel實現(xiàn)無刷新上傳圖片
- asp.net 自定義控件實現(xiàn)無刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖
- ASP.NET FileUpload 上傳圖片實例
- asp.net上傳圖片到服務(wù)器方法詳解
相關(guān)文章
ASP.NET Core 集成 React SPA應(yīng)用的步驟
這篇文章主要介紹了ASP.NET Core 集成 React SPA應(yīng)用的步驟,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-04-04
ASP.NET編程中經(jīng)常用到的27個函數(shù)集
asp.net 整理的27個函數(shù)集,大家可以參考下2008-08-08
關(guān)于中g(shù)ridview 字符串截取的方法
在Gridview中,如果你的某一列字符串的長度過長,不做處理的話.那么將顯示的奇丑無比,可以采取設(shè)置樣式,將其顯示為定長,可以在點擊查看的時候,在另一個頁面對其進(jìn)行顯示2013-06-06
asp.net core 實現(xiàn)一個簡單的倉儲的方法
本篇文章主要介紹了asp.net core 實現(xiàn)一個簡單的倉儲的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
ASP.NET 服務(wù)器路徑和一般資源調(diào)用
ASP.NET 服務(wù)器路徑和一般資源調(diào)用,實現(xiàn)代碼。2009-08-08
asp.net“服務(wù)器應(yīng)用程序不可用” 解決方法
服務(wù)器應(yīng)用程序不可用 您試圖在此 Web 服務(wù)器上訪問的 Web 應(yīng)用程序當(dāng)前不可用。請點擊 Web 瀏覽器中的“刷新”按鈕重試您的請求。 管理員注意事項: 詳述此特定請求失敗原因的錯誤消息可在 Web 服務(wù)器的系統(tǒng)事件日志中找到。請檢查此日志項以查明導(dǎo)致該錯誤發(fā)生的原因。2008-10-10

