stream(流)的相关操作
一直对流的操作有些混乱,今天总结以下。
它主要包括:文件的上传,文件存入数据库(包括加密),从数据库读取文件等等。先一一讨论。有更好的方法欢迎指出。
一.文件上传。代码如下:
二.文件存入数据库。
它主要包括:文件的上传,文件存入数据库(包括加密),从数据库读取文件等等。先一一讨论。有更好的方法欢迎指出。
一.文件上传。代码如下:
1 /// <summary>
2 /// 将文件上传到一个文件夹
3 /// </summary>
4 private void SaveToFileJia()
5 {
6 string fileName = this.FilePhoto.PostedFile.FileName;
7 string path = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath);
8 path +="/upFile/" + "aaa" + fileName.Substring(fileName.IndexOf(".",fileName.Length - fileName.IndexOf(".")));
9 FileStream fs = new FileStream(path,FileMode.Create);
10
11 //得到byte[]类型
12 int iLen = this.FilePhoto.PostedFile.ContentLength;
13 byte[] btPhoto = new byte[iLen];
14 Stream myStream = this.FilePhoto.PostedFile.InputStream;
15 myStream.Read(btPhoto,0,iLen);
16
17 fs.Write(btPhoto,0,iLen);
18 fs.Flush();
19 fs.Close();
20 }
2 /// 将文件上传到一个文件夹
3 /// </summary>
4 private void SaveToFileJia()
5 {
6 string fileName = this.FilePhoto.PostedFile.FileName;
7 string path = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath);
8 path +="/upFile/" + "aaa" + fileName.Substring(fileName.IndexOf(".",fileName.Length - fileName.IndexOf(".")));
9 FileStream fs = new FileStream(path,FileMode.Create);
10
11 //得到byte[]类型
12 int iLen = this.FilePhoto.PostedFile.ContentLength;
13 byte[] btPhoto = new byte[iLen];
14 Stream myStream = this.FilePhoto.PostedFile.InputStream;
15 myStream.Read(btPhoto,0,iLen);
16
17 fs.Write(btPhoto,0,iLen);
18 fs.Flush();
19 fs.Close();
20 }
二.文件存入数据库。
1 private void btnToBase()
2 {
3 //得到byte[]类型
4 int iLen = this.FilePhoto.PostedFile.ContentLength;
5 byte[] btPhoto = new byte[iLen];
6 Stream myStream = this.FilePhoto.PostedFile.InputStream;
7 myStream.Read(btPhoto,0,iLen);
8
9 string sTrSql="insert fileToBase( [file] ) values("+
10 "@file)";
11
12 SqlParameter parFile0 = new SqlParameter("@file",SqlDbType.Image);
13 parFile0.Size = btPhoto.Length;
14 parFile0.Value =btPhoto;
15
16 SqlParameter[] parms = new SqlParameter[1];
17 parms[0] = parFile0;
18
19 SqlCommand cmd = new SqlCommand();
20 cmd.CommandType = CommandType.Text;
21 cmd.Connection = new SqlConnection(ConfigurationSettings.AppSettings["SQLConnString"]);
22 cmd.CommandText = sTrSql;
23 cmd.Parameters.Add(parms[0]);
24
25 cmd.Connection.Open();
26 cmd.ExecuteNonQuery();
27 cmd.Connection.Close();
28
29
30 }
2 {
3 //得到byte[]类型
4 int iLen = this.FilePhoto.PostedFile.ContentLength;
5 byte[] btPhoto = new byte[iLen];
6 Stream myStream = this.FilePhoto.PostedFile.InputStream;
7 myStream.Read(btPhoto,0,iLen);
8
9 string sTrSql="insert fileToBase( [file] ) values("+
10 "@file)";
11
12 SqlParameter parFile0 = new SqlParameter("@file",SqlDbType.Image);
13 parFile0.Size = btPhoto.Length;
14 parFile0.Value =btPhoto;
15
16 SqlParameter[] parms = new SqlParameter[1];
17 parms[0] = parFile0;
18
19 SqlCommand cmd = new SqlCommand();
20 cmd.CommandType = CommandType.Text;
21 cmd.Connection = new SqlConnection(ConfigurationSettings.AppSettings["SQLConnString"]);
22 cmd.CommandText = sTrSql;
23 cmd.Parameters.Add(parms[0]);
24
25 cmd.Connection.Open();
26 cmd.ExecuteNonQuery();
27 cmd.Connection.Close();
28
29
30 }
如果需要加密,可以这样实现:

浙公网安备 33010602011771号