2016-11-28 24 views
-1

我有一些JSON數據,其中包含帶字節的圖像字段。我想將這些字節保存到數據庫中,但我無法將字符串轉換爲字節數組。如何將字符串字節轉換爲字節[]並保存到數據庫中

{ 
    "trackid": "TRXh82URBNA2878934", 
    "serviceid": "66", 
    "category": "URBN", 
    "name": "sachin", 
    "description": "Sanitations Work", 
    "location": "Greater Noida", 
    "requesterlocation": "new delhi", 
    "email": "[email protected]", 
    "contact_number": "882379823", 
    "latitude": "78.23", 
    "longitude": "30.23", 
    "locationtype": "urban", 
    "sectorid": "12", 
    "userid": "34", 
    "filename": "IMG001", 
    "extension": ".jpg", 
    "contenttype": "image/jpeg", 
    "image": "0xFFD8FFE13F5745786966000049492A000800000012000E01020020000000E60000000F01020020000000060100001001020020000000260100001201030001000000010000001A01050001000000460100001B010500010000004E01000028010300010000000200000031010200200000005601000032010200140000007601000013020300010000000200000020020400010000000000000021020400010000000000000022020400010000000000000023020400010000000000000024020400010000000100000025020200200000008A0100006987040001000000AA01000025880400010000002C0300004504000000000000000000000000000000000000000000000000000000000000000000004F50504F000000000000000000000000000000000000000000000000000000005839303039000000000000000000000000000000000000000000000000000000480000000100000048000000010000004D6564696154656B2043616D657261204170706C69636174696F6E0A00000000323031363A31303A32312030393A34383A323200000000000000000000000000000000000000000000000000000000000000000019009A82050001000000DC0200009D82050001000000E40200002288030001000000000000002788030001000000320000000090070004000000303232300390020014000000EC02000004900200140000000003000001910700040000000102030004920A0001000000140300000792030001000000020000000892030001000000FF000000099203000" 
} 

我在將字節串插入數據庫時​​遇到錯誤。這是我的代碼:

SqlCommand cmd = new SqlCommand("PROC_INSERT_CITIZENT_REQUEST_DOCUMENTS_FOR_WEB_API"); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.Connection = Connection; 
Connection.Open(); 
cmd.Parameters.AddWithValue("@P_Cititzen_request_recno", oRequest.requestid); 
cmd.Parameters.AddWithValue("@P_Category", oRequest.category); 
cmd.Parameters.AddWithValue("@P_Filename", oRequest.filename); 
cmd.Parameters.AddWithValue("@P_extension", oRequest.extension); 
cmd.Parameters.AddWithValue("@P_contentType", oRequest.contenttype); 
//byte[] newBytes = Convert.FromBase64String(oRequest.image); 
cmd.Parameters.AddWithValue("@P_Doc_Data", Encoding.UTF8.GetBytes(oRequest.image)); 
cmd.Parameters.AddWithValue("@P_MSG", ""); 

j = cmd.ExecuteNonQuery(); 
if (j < 1) 
{ 
    j = 0; 
} 

我在做什麼錯?

+0

「我面對錯誤」沒有按不要告訴我們任何關於你所看到的錯誤,或者它在哪裏的錯誤。看起來你應該將'image'字段解析爲十六進制 - 關於將十六進制解析爲字節的堆棧溢出有很多問題。 –

+0

oRequest.image的類型是什麼? – Damith

+0

它是字符串類型.. –

回答

0

首先,您的字符串不是Base64編碼的,也不是UTF8編碼的。它是一串十六進制數字。您需要一種將十六進制轉換爲字節的方法。 Stack Overflow已經涵蓋了這個問題。請參見How do you convert Byte Array to Hexadecimal String, and vice versa?

但是,還有一個問題:爲了使用鏈接問題中的一個答案從十六進制字符串轉換,必須首先刪除「0x」前綴,並且十六進制字符串的長度必須是偶數(因爲十六進制數字被成對處理以產生字節)。在你的情況下,刪除前綴後的十六進制字符串的長度是1155,這意味着你缺少一個數字。據推測,由於通常用於指示十六進制數字的「0x」符號允許省略前導零,因此缺失的數字在字符串的開始處爲零。如果這裏是真實的,那麼你應該可以使用下面的代碼來進行轉換:

// strip off "0x" prefix 
string hex = oRequest.image.Substring(2);  

// add leading 0 if length is odd 
if (hex.Length % 2 == 1) hex = "0" + hex;  

// convert hex string to bytes 
byte[] bytes = System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary.Parse(hex).Value; 

然後將其添加到您的SqlCommand這樣的:

cmd.Parameters.AddWithValue("@P_Doc_Data", bytes); 
相關問題