(加急)在数据库中怎么读image类型字段,里面是文字。
解决方案 »
- 关于sql返回所有行(三层结构)
- C#如何写一个打印的功能 模板是OFFICE 但是程序里不用OFFICE的东西
- 怎样设计一个表头表身的窗体?
- 网络编程高手请进帮我读一段代码
- ★★★一个简单的问题,C#中生成DLL时也附带生成XML注释文档,引用的时候为什么没有出现每个方法跟字段等等内容的提示呀?★★★
- 请问c#类的关联关系----关联聚合组合在代码层面的实现是否是一样的
- 俺是个小菜,有个域和变量的问题想请教。
- 我想在一个ASPX文件里放两个FORM能办的到吗?
- 散分了!!!关于datagrid操作数据库的源代码,最简单的即可。分不够再加!
- 如何获得本机IP?在线等。。。。。。。。。。。
- "指定的参数已超出有效值的范围。参数名: 23 " 这是什么错误啊。?
- 哪位知道,怎样遍历一个目录得到所有文件名和文件的路径?
/// download File From Server's DB
/// </summary>
public void downloadFile(HttpRequest reqst,HttpResponse respon, HttpServerUtility server , string userId,string nodeId,string docId,string version,bool isCheckOut)
{
DataBase db = null;
System.IO.FileStream myFile = null;
//BinaryReader br = null;
respon.Clear(); try
{
db= new DataBase();
db.Open(); // Check the File
this.checkFileExisted( db.Connection , null , nodeId , docId , null );
}
catch( ApplicationException ex )
{
throw ex;
}
catch( Exception ex )
{
throw ex;
}
finally
{
db.Close();
} try
{
db= new DataBase();
db.Open(); // DownLoad File
string sqlText = " SELECT DocName, Content ";
sqlText += " FROM T_DMS_DOC ";
sqlText += " WHERE NodeId ='"+DataBase.SqlItemTextChange(nodeId)+"' ";
sqlText += " AND DocId ='"+DataBase.SqlItemTextChange(docId)+"' ";
sqlText += " AND Version ='"+DataBase.SqlItemTextChange(version)+"' ";
SqlCommand cmd=new SqlCommand(sqlText,db.Connection);
SqlDataReader rd;
// Write Log
LogManager.WriteLog("Execute "+cmd.CommandText);
rd=cmd.ExecuteReader(); while(rd.Read())
{
// Orginal File Name
string fileName = rd["DocName"].ToString(); // Temp File Name with server path
string ServerPath = server.MapPath("TempFile"); // Delete temp , if existed
if(File.Exists(ServerPath))
{
File.Delete(ServerPath);
} // Get File from database
myFile = new System.IO.FileStream(ServerPath,System.IO.FileMode.Create);
myFile.Write(((byte[])rd["Content"]),0,((byte[])rd["Content"]).Length); #region New Source
// Write file into harddisk
StreamWriter swMyFile = new StreamWriter( myFile ) ;
swMyFile.WriteLine("") ;
swMyFile.Flush() ;
swMyFile.Close() ;
myFile.Close() ; // Get Temp File info
FileInfo fi = new FileInfo(ServerPath); // determine the name and size of the file
string filename = fi.Name;
string filesize = fi.Length.ToString();
fi = null; // clear the response, set the contenttype and the file name/size
respon.Clear();
respon.ContentType = "application/octet-stream";
respon.AppendHeader("Content-Disposition", "attachment; filename=" + server.UrlEncode(fileName) + @"");
respon.AppendHeader ("Content-Length", filesize); // flush the response, then send the file down
respon.Flush();
respon.WriteFile(ServerPath);
#endregion #region Old Source
// br = new BinaryReader(myFile);
//
// //respon.Clear();
// respon.AddHeader("Accept-Ranges", "bytes");
// respon.Buffer = false;
// long fileLength = myFile.Length;
// long startBytes = 0;
//
// int pack = 10240; //10K bytes
// //int sleep = 200;
// int sleep = (int)Math.Floor(1000 * pack / 1048576) + 1;
// if (reqst.Headers["Range"] != null)
// {
// respon.StatusCode = 206;
// string[] range = reqst.Headers["Range"].Split(new char[] {'=', '-'});
// startBytes = Convert.ToInt64(range[1]);
// }
// respon.AddHeader("Content-Length", (fileLength - startBytes).ToString());
// if (startBytes != 0)
// {
// respon.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
// }
//
// respon.AddHeader("Connection", "Keep-Alive");
// respon.ContentType = "application/octet-stream";
// respon.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8).Replace("+"," ") );
//
// br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
// int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;
//
// respon.Flush();
//
// for (int i = 0; i < maxCount; i++)
// {
// if (respon.IsClientConnected)
// {
// respon.BinaryWrite(br.ReadBytes(pack));
// Thread.Sleep(sleep);
// }
// else
// {
// i=maxCount;
// }
// }
//
// br.Close();
// myFile.Close();
#endregion
} rd.Close();
db.Close();
}
catch(ApplicationException ex)
{
throw ex;
}
catch(Exception ex)
{
throw ex;
}
finally
{
// db.Close();
respon.End();
}
}
/// Creat New File in DB
/// </summary>
/// <param name="strNodeId"></param>
/// <param name="file"></param>
/// <param name="description"></param>
/// <param name="userId"></param>
public void creatNewFile(string strtitle,HtmlInputFile file)
{
Stream imgdatastream = file.PostedFile.InputStream;
string fileName = System.IO.Path.GetFileName(file.PostedFile.FileName).Trim();
int imgdatalen = file.PostedFile.ContentLength;
string imgtype = file.PostedFile.ContentType;
byte[] imgdata = new byte[imgdatalen];
int n = imgdatastream.Read(imgdata,0,imgdatalen);
DataBase db = new DataBase();
// DataSet ds ;
db.Open();
// Get Document ID
double randomStr = new System.Random().NextDouble()* 1000000000;
string docID = DateTime.Now.ToString("yyyyMMdd") + (Math.Floor(randomStr)).ToString(); if(imgdatalen ==0)
{return;}
// Insert File to DB
string sql = " INSERT INTO FILE2(TITLE,MARK) ";
sql += " VALUES('"+DataBase.SqlItemTextChange(strtitle) +"',@imgdata)" ; SqlCommand command = new SqlCommand(sql,db.Connection ); SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
paramData.Value = imgdata;
command.Parameters.Add( paramData ); try
{
// db.BeginTrans(); int numRowsAffected = command.ExecuteNonQuery(); if(numRowsAffected !=1)
{
throw new Exception("1012");
}
//db.CommitTrans();
}
catch(Exception ex)
{
db.RollBack();
throw ex;
}
finally
{
db.Close();
}
} /// <summary>
/// download File From Server's DB
/// </summary>
public void downloadFile()
{
DataBase db = null;
System.IO.FileStream myFile = null;
try
{
db= new DataBase();
db.Open(); // DownLoad File
string sqlText = " SELECT TITLE, MARK ";
sqlText += " FROM FILE2 ";
SqlCommand cmd=new SqlCommand(sqlText,db.Connection);
SqlDataReader rd;
rd=cmd.ExecuteReader(); while(rd.Read())
{
// Orginal File Name
string strtitle = rd["TITLE"].ToString(); string strfile = System.Text.Encoding.Default.GetString((byte[])rd["MARK"]);// Label1.Text = this.Server.HtmlEncode(strfile);
// testArea.Value = this.Server.HtmlEncode(strfile); #endregion
} rd.Close();
db.Close();
}
catch(ApplicationException ex)
{
throw ex;
}
catch(Exception ex)
{
throw ex;
}
finally
{
// db.Close();
}
}