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">
</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" C
- 本文实例分析了Python作用域用法。分享给大家供大家参考,具体如下:每一个编程语言都有变量的作用域的概念,Python也不例外,以下是Py
- 关闭正在运行的 MySQL :[root@www.woai.it ~]# service mysql stop运行[root@www.woa
- 先来看一个栗子EXPLAIN select * from employees where name > 'a';如果用
- listlist是一种有序的集合,可以随时添加和删除其中的元素。跟java不一样的是 可以使用arr[-1] 0>-x >=-
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 4 - Functions函数和MooTools
- Array()CInt()CStr() Date() DateAdd() Day() FormatCurrency() FormatDate
- 在使用google或者baidu搜图的时候会发现有一个图片颜色选项,感觉非常有意思,有人可能会想这肯定是人为的去划分的,呵呵,有这种可能,但
- MySQL手册中find_in_set函数的语法解释:FIND_IN_SET(str,strlist)str 要查询的字符串 strlist
- 本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下#!/usr/bin/env python# -*- c
- 问题你想使用一个Python字典存储数据,并将它转换成XML格式。解决方案尽管 xml.etree.ElementTree 库通常用来做解析
- asp之家注:本篇asp留言本教程可以说是我见过的最详细,写的最认真的,最适合初学者来学习asp的一篇教程。在此感谢作者hemooday。可
- 这篇文章主要介绍了Python类如何定义私有变量,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以
- 方法一: 代码如下:id=request.form("checkbox") id=Split(id,"
- 如果仅仅是定义表格的边框为1(border="1")和边框颜色值(如borderC
- 方案概要: 1. 改变文件存储时的文件名 2. 配置索引服务器,并将索引服务器与MS SQL Server关联。 3. 修改SQL语句,将进
- 什么是pyscriptpyscript由来自 Anaconda 的团队开发,包括 Peter Wang、Fabio Pliger 和 Phi
- 最近参与了将一个Sybase数据库移植到Microsoft SQL Server 2000上的项目,我在这一项目上获得的经验,将对Sybas
- 这篇文章主要介绍了Python代码块及缓存机制原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 了解了HTTP协议和HTML文档,我们其实就明白了一个Web应用的本质就是: 浏览器发送一个HTTP请求