如何使用JSON WebService WP7在数据库中保存图像

本文关键字:数据库 保存 图像 WP7 何使用 JSON WebService | 更新日期: 2023-09-27 18:29:26

我想用JSON web服务在数据库中保存图像。不保存图像数据。但当发送图像字节时,它不会被保存。如何在窗口电话-7 中使用JSon 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);
        }

我的代码有任何错误。请帮助。。。

如何使用JSON WebService WP7在数据库中保存图像

让我猜测:是否正在发出一个空请求?

如果您只是使用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);
    });
}