Как сохранить изображение в базе данных с помощью JSON WebService WP7 - PullRequest
0 голосов
/ 29 марта 2012

Я хочу сохранить изображение в базе данных с помощью веб-службы JSON. Без сохранения данных изображения. Но при отправке изображения байт не сохраняется. Как отправить или получить изображение с помощью веб-службы JSon в окне телефона-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 голосов
/ 29 марта 2012

Дайте угадаю: пустой запрос делается?

Если вы просто используете 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);
    });
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...