.net下Oracle的Blob字段存取(二)上传下载
上传
和前一篇差不多,不过先插入的是文本格式,这对blob是大材小用了,现在上传附件代码:
1 //新增文件之-----------存文件到数据库
2 public void attach(string filename,string fn,string username,string title )
3 {Users.Class1 u=new Users.Class1();//得到用户ID
4 int uid = u.getUserID(username);
5 Papers.papers p = new papers();
6 int paper_id=p.getpaperid(title, uid);//得到论文ID
7 string sql = "update paper_table set attach=:aa,attachfilename=:bb where ID='"+paper_id+"'";
8 OracleCommand cmd = new OracleCommand(sql, conn);
9 conn.Open();
10 FileStream fs = File.OpenRead(filename);
11 byte[] b = new byte[fs.Length];
12 fs.Read(b, 0, b.Length);
13 fs.Close();
14
15 cmd.Parameters.Add("aa", System.Data.OracleClient.OracleType.BFile, b.Length).Value = b;
16 cmd.Parameters.Add("bb", System.Data.OracleClient.OracleType.VarChar).Value = fn;
17 cmd.ExecuteNonQuery();
18 conn.Close();
19 }
2 public void attach(string filename,string fn,string username,string title )
3 {Users.Class1 u=new Users.Class1();//得到用户ID
4 int uid = u.getUserID(username);
5 Papers.papers p = new papers();
6 int paper_id=p.getpaperid(title, uid);//得到论文ID
7 string sql = "update paper_table set attach=:aa,attachfilename=:bb where ID='"+paper_id+"'";
8 OracleCommand cmd = new OracleCommand(sql, conn);
9 conn.Open();
10 FileStream fs = File.OpenRead(filename);
11 byte[] b = new byte[fs.Length];
12 fs.Read(b, 0, b.Length);
13 fs.Close();
14
15 cmd.Parameters.Add("aa", System.Data.OracleClient.OracleType.BFile, b.Length).Value = b;
16 cmd.Parameters.Add("bb", System.Data.OracleClient.OracleType.VarChar).Value = fn;
17 cmd.ExecuteNonQuery();
18 conn.Close();
19 }
下载
先把下载好的二进制转换成以前存过数据库的格式,即刚存的时候要保存文件的扩展名,下载还原的时候用.
转换后的文件丰在临时的文件夹,后来直接response.redirect("*.*")就可以下载了.
1 //下载文件-收集于网上只是注解-以下是针对Oracle数据库,SQL server一样
2 public string download(string pid,string path)
3 {
4
5 FileStream objFs;
6
7 BinaryWriter objBw;
8
9 int bufferSize = 260000;//这个是缓冲区大小,设置太小,小于下载的文件大小则会出错
10
11 byte [] outByte = new byte[bufferSize];
12
13 long retval;
14
15 long startIndex = 0;
16
17 string fileName = "";
18
19 string sql = "select * from paper_table where id='" + pid + "'";//从数据库取
20 OracleCommand cmd = new OracleCommand(sql, conn);
21 conn.Open();
22 OracleDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
23 dr.Read();
24
25 fileName = dr["attachfilename"].ToString();//文件名
26 string tempfilename = fileName;
27 fileName =path+fileName;
28 objFs = new FileStream(fileName, FileMode.OpenOrCreate,FileAccess.Write);
29
30 objBw = new BinaryWriter(objFs);
31
32 startIndex = 0;
33
34 retval = dr.GetBytes(8, startIndex, outByte, 0, bufferSize);
35
36 while ( retval == bufferSize)
37 {
38 objBw.Write(outByte);
39
40 objBw.Flush();
41
42 startIndex += bufferSize;
43
44 retval = dr.GetBytes(8, startIndex, outByte, 0, bufferSize);
45 }
46
47 objBw.Write(outByte, 0, (int)retval -1);
48
49 objBw.Flush();
50
51 objBw.Close();
52
53 objFs.Close();
54
55 dr.Close();
56
57 conn.Close();
58 return tempfilename;
59
60
61
62
63
64
65
66 }
67
2 public string download(string pid,string path)
3 {
4
5 FileStream objFs;
6
7 BinaryWriter objBw;
8
9 int bufferSize = 260000;//这个是缓冲区大小,设置太小,小于下载的文件大小则会出错
10
11 byte [] outByte = new byte[bufferSize];
12
13 long retval;
14
15 long startIndex = 0;
16
17 string fileName = "";
18
19 string sql = "select * from paper_table where id='" + pid + "'";//从数据库取
20 OracleCommand cmd = new OracleCommand(sql, conn);
21 conn.Open();
22 OracleDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
23 dr.Read();
24
25 fileName = dr["attachfilename"].ToString();//文件名
26 string tempfilename = fileName;
27 fileName =path+fileName;
28 objFs = new FileStream(fileName, FileMode.OpenOrCreate,FileAccess.Write);
29
30 objBw = new BinaryWriter(objFs);
31
32 startIndex = 0;
33
34 retval = dr.GetBytes(8, startIndex, outByte, 0, bufferSize);
35
36 while ( retval == bufferSize)
37 {
38 objBw.Write(outByte);
39
40 objBw.Flush();
41
42 startIndex += bufferSize;
43
44 retval = dr.GetBytes(8, startIndex, outByte, 0, bufferSize);
45 }
46
47 objBw.Write(outByte, 0, (int)retval -1);
48
49 objBw.Flush();
50
51 objBw.Close();
52
53 objFs.Close();
54
55 dr.Close();
56
57 conn.Close();
58 return tempfilename;
59
60
61
62
63
64
65
66 }
67