我開發調用由第三方開發的從客戶數據庫發送/接收數據的Web服務的Web應用程序。我正在使用ASP.NET 3.5 C#構建應用程序。BLOB保存到磁盤圖像C#
,他們給我提供圖像的方式是在BLOB格式。我打電話給他們的方法,我回來的是一個DataSet,它有兩個字段「logo_name」和「logo」,它是BLOB字段。 我想要做的是:在本地保存映像的磁盤上(以儘量減少在未來調用他們的數據庫)。
我一直在玩弄一對夫婦這樣做的不同的方法,但似乎無法得到的圖像以正確保存。我已經能夠以正確的名稱在文件夾中創建文件,但如果我嘗試查看圖像,則無法正常工作。
我希望有人可以給我一些示例代碼來告訴我怎麼BLOB字段保存到本地磁盤?
下面是代碼我現在有
public static class ProductImages
{
public static string GetSchoolLogo(string schoolID, string printCodeID)
{
int v_length = 0;
string v_file_name = "";
byte[] v_file_data = null;
try
{
//Declare Web Service Variables
P_SERVICE.Service ldeService = new P_SERVICE.Service();
//Authentication Header
ldeService.AuthHeaderValue = CGlobal.GetAuthHeader(System.Web.HttpContext.Current.Session);
P_SERVICE.CDataResultOfDataSet ldeResult = null;
DataSet ds = new DataSet();
ldeResult = ldeService.GetItemLogo(schoolID, printCodeID);
ds = ldeResult.Value;
if (ds.Tables[0].Rows.Count > 0)
{
v_file_data = StrToByteArray(ds.Tables[0].Rows[0]["logo"].ToString().TrimEnd());
v_file_name = ds.Tables[0].Rows[0]["logo_name"].ToString().TrimEnd();
v_length = Convert.ToInt32(v_file_data.Length);
// Load the file in the Memory Stream
MemoryStream ms = new MemoryStream(v_file_data, 0, v_file_data.Length);
ms.Write(v_file_data, 0, v_file_data.Length);
// Open the file stream in ordre to save on the local disk
string path = HttpContext.Current.Server.MapPath("~/_imagecache/schoolLogos/").ToString();
FileStream fs = File.OpenWrite(path + schoolID + "/" + v_file_name);
fs.Write(v_file_data, 0, v_file_data.Length);
fs.Close();
// Return True if no errors occured
return "~/_imagecache/schoolLogos/" + schoolID + "/" + v_file_name;
}
else
return "~/images/noPhoto.gif";
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
//return "~/images/noPhoto.gif";
}
}
// C# to convert a string to a byte array.
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
}
}
當我返回的數據類型,它返回System.String
Type t = ds.Tables[0].Rows[0]["logo"].GetType();
HttpContext.Current.Trace.Warn(t.FullName);
感謝
您可以發佈您當前的代碼,它可能會更容易地指出其中的問題是不是希望有人張貼代碼爲您服務。 – Pharabus 2010-04-29 17:27:30
同意Pharabus,發佈你的代碼,我相信你至少有一個字節數組和文件流開始 - 向我們展示你的代碼。 – kd7 2010-04-29 17:37:46
謝謝你們,我的代碼在上面。 – TGuimond 2010-04-29 17:39:58