读取文件夹,将文件直接保存到数据库中
前台文件


1
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="main.aspx.cs" Inherits="CommPage_main" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>无标题页</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
<table style="border:0; 100%; text-align:right;">
13
<tr>
14
<td style="height: 26px"></td>
15
<td style="height: 26px"></td>
16
<td style="height: 26px"><asp:Button ID="btnSaveToDataBase" runat="server" Height="24px" OnClick="btnSaveToDataBase_Click" Text="图片保存到数据库中" Width="176px" /></td>
17
</tr>
18
</table>
19
</div>
20
21
</form>
22
</body>
23
</html>
24
CS文件


1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
12
using System.Data.SqlClient;
13
using System.IO;
14
using System.Drawing;
15
using System.Drawing.Imaging;
16
using SuntownBase;
17
18
public partial class CommPage_main : System.Web.UI.Page
19

{
20
STShared ST = new STShared();
21
22
protected void Page_Load(object sender, EventArgs e)
23
{
24
25
}
26
27
protected void btnSaveToDataBase_Click(object sender, EventArgs e)
28
{
29
int i = 0;//文件计数
30
31
string[] dirs = Directory.GetFiles(@"D:\aaa\images");
32
if (dirs.Length != 0)
33
{
34
try
35
{
36
//Response.Write("共有" + dirs.Length + "个文件!");
37
foreach (string dir in dirs)
38
{
39
//Response.Write("<p>" + dir);
40
//如果文件类型不匹配,则重新循环
41
if ((dir.ToLower()).IndexOf(".jpg") == -1 && (dir.ToLower()).IndexOf(".gif") == -1)
42
{
43
continue;
44
}
45
else
46
{
47
i++; //文件计数 加1
48
}
49
50
//C:\Inetpub\wwwroot\CadFormula\images\item_12.jpg
51
52
//FileInfo fi = new FileInfo(openFileDialog1.PostedFile.FileName);
53
54
FileInfo fi = new FileInfo(dir);
55
int imgdatalen = (int)fi.Length;
56
byte[] imgdata = new byte[imgdatalen];
57
Stream imgdatastream = fi.OpenRead();
58
int n = imgdatastream.Read(imgdata, 0, imgdatalen);
59
string ConnectionString = "server=.;database=image;uid=sa;pwd=admin";
60
SqlConnection conn = new SqlConnection(ConnectionString);
61
62
string sql = "insert into [image] (picture) values (@imgdata)";
63
64
SqlCommand cmd = new SqlCommand(sql, conn);
65
66
SqlParameter paramData = new SqlParameter("@imgdata", SqlDbType.Image);
67
paramData.Value = imgdata;
68
cmd.Parameters.Add(paramData);
69
70
conn.Open();
71
cmd.ExecuteNonQuery();
72
conn.Close();
73
}
74
}
75
catch (Exception err)
76
{
77
Response.Write("出现错误:" + err.Message.ToString());
78
}
79
finally
80
{
81
if(i == 0)
82
{
83
ST.MessageBox(Page,"没有匹配的文件!","确定");
84
}
85
else
86
{
87
ST.MessageBox(Page, "成功上传" + i.ToString() + "个文件!","确定");
88
}
89
//最后删除此文件夹下的文件
90
}
91
}
92
}
93
}
94
===============================================
下面是读取文件
前台:


1
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="CommPage_test2" %>
2
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>无标题页</title>
8
</head>
9
<body>
10
<form id="form1" runat="server">
11
<div>
12
<img id=ImgSpan1 border=0 src="testimg/select.jpg"><br>
13
<img id=img border=0>
14
<br />
15
<input type=button value=顺转90度 onclick="document.all.ImgSpan.style.filter='progid:DXImageTransform.Microsoft.BasicImage(Rotation=1)'" id="Button3">
16
<input type=button value=顺转180度 onclick="document.all.ImgSpan.style.filter='progid:DXImageTransform.Microsoft.BasicImage(Rotation=2)'">
17
<input type=button value=逆转90度 onclick="document.all.ImgSpan.style.filter='progid:DXImageTransform.Microsoft.BasicImage(Rotation=3)'">
18
<input type=button value=恢复角度 onclick="document.all.ImgSpan.style.filter='progid:DXImageTransform.Microsoft.BasicImage(Rotation=0)'">
19
<br />
20
<br />
21
<br />
22
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="图片保存到数据库中" Height="24px" Width="176px" /><br />
23
<br />
24
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="显示图片" /><br />
25
<br />
26
<br />
27
</div>
28
</form>
29
</body>
30
</html>
31
32
33
<script language="javascript">
34
<!--
35
function bbimg(o)
36
{
37
var zoom=parseInt(o.style.zoom, 10)||100;
38
zoom+=event.wheelDelta/12;
39
if (zoom>0) o.style.zoom=zoom+'%';
40
}
41
42
function addthis()
43
{
44
var e=window.event?window.event:e;
45
var srcE=e.srcElement?e.srcElement:e.target;
46
if(!srcE) return;
47
if (String(srcE.tagName).toLowerCase()=='img')
{
48
bbimg(srcE)
49
}
50
}
51
52
function backthis()
53
{
54
var e=window.event?window.event:e;
55
var srcE=e.srcElement?e.srcElement:e.target;
56
if(!srcE) return;
57
if ((String(srcE.tagName).toLowerCase()=='img')&&(window.event.ctrlKey))
58
{
59
srcE.style.zoom='100%';
60
}
61
}
62
document.onmousewheel=addthis;
63
document.onmouseout=backthis;
64
//-->
65
</script>
66
下面是后台文件CS


1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
12
using System.Data.SqlClient;
13
using System.IO;
14
using System.Drawing;
15
using System.Drawing.Imaging;
16
17
public partial class CommPage_test2 : System.Web.UI.Page
18

{
19
protected void Page_Load(object sender, EventArgs e)
20
{
21
22
}
23
24
protected void Button1_Click(object sender, EventArgs e)
25
{
26
string[] dirs = Directory.GetFiles(@"D:\aaa\images");
27
if(dirs.Length !=0)
28
{
29
try
30
{
31
Response.Write("共有" + dirs.Length + "个文件!");
32
foreach (string dir in dirs)
33
{
34
Response.Write("<p>" + dir);
35
//C:\Inetpub\wwwroot\CadFormula\images\item_12.jpg
36
37
//FileInfo fi = new FileInfo(openFileDialog1.PostedFile.FileName);
38
39
FileInfo fi = new FileInfo(dir);
40
int imgdatalen = (int)fi.Length;
41
byte[] imgdata = new byte[imgdatalen];
42
Stream imgdatastream = fi.OpenRead();
43
int n = imgdatastream.Read(imgdata, 0, imgdatalen);
44
string ConnectionString = "server=.;database=image;uid=sa;pwd=admin";
45
SqlConnection conn = new SqlConnection(ConnectionString);
46
47
//try '"+comment+"',"+imgdata+"
48
//{
49
string sql = "insert into [image] (picture) values (@imgdata)";
50
51
SqlCommand cmd = new SqlCommand(sql, conn);
52
53
SqlParameter paramData = new SqlParameter("@imgdata", SqlDbType.Image);
54
paramData.Value = imgdata;
55
cmd.Parameters.Add(paramData);
56
57
conn.Open();
58
cmd.ExecuteNonQuery();
59
conn.Close();
60
}
61
}
62
catch (Exception err)
63
{
64
Response.Write("出现错误:" + err.Message.ToString());
65
}
66
finally
67
{
68
Response.Write("<p>======================================");
69
Response.Write("<p>成功上传<font color=#0ff>"+ dirs.Length +"</font>个文件!");
70
//最后删除此文件夹下的文件
71
}
72
}
73
}
74
protected void Button2_Click(object sender, EventArgs e)
75
{//读取图片显示
76
MemoryStream stream = new MemoryStream();
77
SqlConnection conn = new SqlConnection("server=.;database=image;uid=sa;pwd=admin");
78
79
conn.Open();
80
81
SqlCommand cmd = new SqlCommand("select top 1 picture from [image] order by id desc", conn);
82
byte[] image = (byte[])cmd.ExecuteScalar();
83
stream.Write(image, 0, image.Length);
84
Bitmap bitmap = new Bitmap(stream);
85
Response.ContentType = "image/Jpeg";
86
bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
87
conn.Close();
88
stream.Close();
89
}
90
}
91