将BLOB保存到磁盘为Image C#

时间:2010-04-29 17:24:07

标签: c# asp.net image webforms blob

我正在开发一个Web应用程序,它调用由第三方开发的Web服务来从客户端数据库发送/接收数据。我正在使用ASP.NET 3.5 C#构建应用程序。

他们为我提供图像的方式是BLOB格式。我调用他们的方法,我得到的是一个带有2个字段“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);

由于

3 个答案:

答案 0 :(得分:2)

你的专栏:

v_file_data = StrToByteArray(ds.Tables[0].Rows[0]["logo"].ToString().TrimEnd()); 

应该是:

v_file_data = ds.Tables[0].Rows[0]["logo"] as Byte[];

您使用的方法不会以相同的方式对字节进行编码和解码。

答案 1 :(得分:1)

好的,所以看起来Web服务对你没有好处。他们真的应该返回一个字节数组。但无论如何,字符串绝对不是base64,绝对不是ascii。

让我们在黑暗中尝试这种疯狂的刺:

v_file_data = Encoding.UTF8.GetBytes(ds.Tables[0].Rows[0]["logo"]);

答案 2 :(得分:0)

试试这个:

// replace this line:
//v_file_data = StrToByteArray(ds.Tables[0].Rows[0]["logo"].ToString().TrimEnd());

// with this block:
string base64string = ds.Tables[0].Rows[0]["logo"] as string;

if (base64string != null)
{
    v_file_data = System.Convert.FromBase64String(v_file_data);
}

获取字节数组并使用其余代码将其加载到文件中。

... HTH

相关问题