如何使用JSON Web服务WP7在DataBase中保存图像

时间:2012-03-29 09:05:51

标签: c# json web-services windows-phone-7 httpwebrequest

我想使用JSON Web服务在数据库中保存图像。没有图像数据保存。 但是当发送图像字节时它不能保存。如何使用JSon webservie在window phone-7

中发送或重放图像

我的网络服务:

    [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);
        }

我的代码中有任何问题。请帮忙......

1 个答案:

答案 0 :(得分: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);
    });
}