2012-03-29 73 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

相关问题