我开发调用由第三方开发的从客户数据库发送/接收数据的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