asp.net上传图片保存到数据库的代码

时间:2024-01-16 05:00:37 

数据库:保存图片的数据格式 图象二进制数据储存字段
前台:


<%@ 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">
&nbsp;
</td>
</tr>
<tr>
<td class="style2">
<input id="UploadImage" name = "UploadImage" type="file" runat="server" onchange="checkdata()" />
</td>
<td class="style2">
&nbsp;
<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>
&nbsp;
</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 = "图片上传成功";
}
}
}
标签:上传图片,数据库
0
投稿

猜你喜欢

  • 深入了解Python enumerate和zip

    2021-11-15 12:08:23
  • mat矩阵和npy矩阵实现互相转换(python和matlab)

    2023-10-19 17:12:02
  • 解决tensorflow 释放图,删除变量问题

    2023-08-10 09:42:47
  • linux安装Python3.4.2的操作方法

    2022-06-17 19:19:15
  • MySQL安装与创建用户操作(新手入门指南)

    2024-01-28 05:35:19
  • Django配置MySQL数据库的完整步骤

    2023-07-19 14:43:47
  • python基础教程之udp端口扫描

    2022-01-10 01:03:22
  • 详解python内置常用高阶函数(列出了5个常用的)

    2023-01-11 18:52:19
  • Golang通脉之数据类型详情

    2023-07-14 05:37:03
  • python sklearn库实现简单逻辑回归的实例代码

    2022-06-19 18:43:29
  • 如何使用Pytorch搭建模型

    2022-07-18 10:34:27
  • Golang解析JSON遇到的坑及解决方法

    2024-05-10 13:58:29
  • bootstrap-table组合表头的实现方法

    2024-05-11 09:07:53
  • Python sklearn中的K-Means聚类使用方法浅析

    2022-03-16 22:01:16
  • golang如何修改json文件内容的方法示例

    2024-04-26 17:32:44
  • CSS布局之浮动(二)三列浮动

    2008-08-19 12:47:00
  • js实现tab选项卡函数代码

    2024-04-19 10:43:25
  • python实现简单学生信息管理系统

    2022-01-28 12:40:00
  • vue中echarts的用法及与elementui-select的协同绑定操作

    2024-05-10 14:20:13
  • 用python实现打砖块小游戏

    2021-11-10 02:52:06
  • asp之家 网络编程 m.aspxhome.com