#region 將文件保存到本地 /// <summary> /// 將文件保存到本地 /// </summary> /// <param name="psContent">文件的二進制數據字符串</param> /// <param name="psFileName">文件名稱,必須帶後綴</param> private void SaveFile(string psContent, string psFileName) { byte[] accessory = Convert.FromBase64String(psContent); //System.AppDomain.CurrentDomain.BaseDirectory獲取程序的基目錄 string vsAccessoryPath = System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + '\\' + psFileName; FileStream fileStream = null; try { //File.Create Method (String):Creates or overwrites a file in the specified path. fileStream = File.Create(vsAccessoryPath); } catch (System.IO.IOException e) { } //FileStream.Write Method:Writes a block of bytes to the file stream. fileStream.Write(accessory, 0, accessory.Length); //FileStream.Flush 方法:清除該流的所有緩沖區,使得所有緩沖的數據都被寫入到基礎設備。 fileStream.Flush(); //FileStream.Close Method:Closes the file and releases any resources associated with the current file stream. fileStream.Close(); } #endregion
本文URL:http://www.bianceng.cn/Programming/csharp/201410/45587.htm
假如文件流保存在數據庫中:
string vsSql = "";//從數據庫中獲取待轉換保存文件的內容(比如,之前把文件轉換為字節流保存到數據庫中了)
DataSet dsContent = 獲取DataSet的數據庫操作;
byte[] vbContent = (byte[])(dsContent.Tables[0].Rows[0]["數據庫中保存文件內容的列名"]);
string vsContent = Convert.ToBase64String(vbContent);
字節流保存在數據庫中的樣子: