2012-03-29 69 views
0

我想用JSON Web服務保存Image中的數據,而不保存圖像數據。 但發送圖像字節時不保存。如何在窗口JSON webservie發送或Recive影像電話7如何使用JSON WebService保存數據庫中的圖像WP7

我的web服務:

[WebMethod] 
      [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
      public string Register(string emailID, string pwd, string name, string img) 
      { 
       ProfileDL _client = new ProfileDL(); 

       _client.Email = emailID; 
       _client.Password = pwd; 
      img = img.Replace(' ', '+'); 
      _client.Firstname = name; 
      _client.Img = Convert.FromBase64String(img); 
      _client.saveData(); 
      return "Y"; 
     } 

WP7 Code:- 



    //Convert Image to byte code 
    private void photoChooserTask_Completed(object sender, PhotoResult e) 
     { 
    imageBytes = new byte[e.ChosenPhoto.Length]; 
    e.ChosenPhoto.Read(imageBytes, 0, imageBytes.Length); 
    } 

void GetRequestStreamCallbackx(IAsyncResult asynchronousResult) 
     { 
      HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState; 
      // End the stream request operation 
      Stream postStream = webRequest.EndGetRequestStream(asynchronousResult); 
      string img = string.Empty; 
      try 
      { 
       img = Convert.ToBase64String(imageBytes); 
      } 
      catch { } 
      // Create the post data 
     // string postData = ""; 
      var json=""; 

       Dispatcher.BeginInvoke(() => json = "{\"emailID\": " + txtemail.Text.Trim() + ",\"pwd\": " + txtpassword.Text + ",\"name\":" + txtname.Text + ",\"img\": " + img + "}"); 


      byte[] byteArray = Encoding.UTF8.GetBytes(json); 

      // Add the post data to the web request 
      try 
      { 
       postStream.Write(byteArray, 0, byteArray.Length); 
      } 
      catch { } 
      postStream.Close(); 

      // Start the web request 
      webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest); 
     } 

有任何事情錯在我的代碼。請幫忙...

回答

0

讓我猜猜:一個空的請求正在進行?

如果您只是使用Dispatcher.BeginInvoke()來設置json變量,它可能會在撥打Encoding.UTF8.GetBytes(json)後設置!

試試這樣說:

void GetRequestStreamCallbackx(IAsyncResult asynchronousResult) 
{ 
    HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState; 
    // End the stream request operation 
    Stream postStream = webRequest.EndGetRequestStream(asynchronousResult); 
    string img = string.Empty; 
    try 
    { 
     img = Convert.ToBase64String(imageBytes); 
    } 
    catch { } 
    // Create the post data 
    // string postData = ""; 
    var json = ""; 

    Dispatcher.BeginInvoke(() => 
    { 
     json = "{\"emailID\": " + txtemail.Text.Trim() + ",\"pwd\": " + txtpassword.Text + ",\"name\":" + txtname.Text + ",\"img\": " + img + "}"; 


     byte[] byteArray = Encoding.UTF8.GetBytes(json); 

     // Add the post data to the web request 
     try 
     { 
      postStream.Write(byteArray, 0, byteArray.Length); 
     } 
     catch { } 
     postStream.Close(); 

     // Start the web request 
     webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest); 
    }); 
} 
+0

感謝先生,我有變化的代碼,根據你,但返回錯誤「AsyncWaitHandle =‘asynchronousResult.AsyncWaitHandle’扔類型的異常的‘System.NotSupportedException’」 – 2012-03-29 10:52:46

+0

確定,然後只是嘗試獲得JSON值之前做孔請求!這應該解決問題! – 2012-03-29 11:17:59