发布于 2016-06-03 09:18:46 | 131 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ASP.NET

ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。


有时候某种需要将图片保存到数据库中,那么下面的代码就可以参考下,下面没有数据库的建表说明,但数据库需要建立下。
数据库:保存图片的数据格式 图象二进制数据储存字段
前台:
 
<%@ 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 = "图片上传成功"; 
} 
} 
} 


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务