2010-04-29 159 views
1

我開發調用由第三方開發的從客戶數據庫發送/接收數據的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); 

感謝

+1

您可以發佈您當前的代碼,它可能會更容易地指出其中的問題是不是希望有人張貼代碼爲您服務。 – Pharabus 2010-04-29 17:27:30

+0

同意Pharabus,發佈你的代碼,我相信你至少有一個字節數組和文件流開始 - 向我們展示你的代碼。 – kd7 2010-04-29 17:37:46

+0

謝謝你們,我的代碼在上面。 – TGuimond 2010-04-29 17:39:58

回答

1

好了,所以它看起來像Web服務是做你任何好處。他們真的應該返回一個字節數組。但無論如何,該字符串絕對不是base64,絕對不是ascii。

讓我們試着在黑暗今野刺:

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

我試過,但我得到了相同的結果。正在創建圖像文件,正確命名和路徑正確返回但不顯示,當我嘗試在任何編輯器中打開時,它會給出未知的文件類型錯誤。圖像大小似乎是正確的。 – TGuimond 2010-04-29 19:44:15

+0

@sky:注意:我添加了一個'ToString()',因爲它給了我一個沒有它的錯誤: 'v_file_data = Encoding.UTF8.GetBytes(ds.Tables [0] .Rows [0] [「logo」 ] .ToString());' – TGuimond 2010-04-29 19:45:01

+0

@Tgui - 提示:'一個錯誤'並沒有太多的工作。至少給出錯誤類型。好吧,試試'v_file_data = Encoding.UTF8.GetBytes(((string)ds.Tables [0] .Rows [0] [「logo」]));' – 2010-04-29 19:49:47

2

你行:

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

應該是:

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

您使用的方法不會以相同的方式對字節進行編碼和解碼。

+0

當然,StrToByteArray很拗口,但也許圖像在Base64中出現? – 2010-04-29 17:49:11

+0

我改變了我的代碼中的那一行,但得到一個空引用異常.. – TGuimond 2010-04-29 18:15:09

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 ....