简体   繁体   English

如何将图像base64发送到服务器Windows Phone 8.1

[英]How to send an image base64 to server windows phone 8.1

I'm developing an application on windows phone 8.1 in c# that can take a picture and send it to a server (in base64) with a post request. 我正在用C#在Windows Phone 8.1上开发一个应用程序,该应用程序可以拍照并通过发布请求将其发送到服务器(在base64中)。

So my problem is that i can't find a method which still work for include my image base64 in the body on my request and send it to my server. 所以我的问题是我无法找到一种方法仍可工作,我可以根据自己的请求将imagebase64包含在正文中并将其发送到服务器。

private async  void validPicture_Click(object sender, RoutedEventArgs e)
    {
        Encode("ms-appx:///xx/xxx.jpg");
        try
        {
            var client = new Windows.Web.Http.HttpClient();
            var uri = new Uri("http://x.x.x.x:x/picture/");
            string pathBase64 = Encode("ms-appx:///xx/xxx.jpg").ToString();

            Dictionary<string, string> pairs = new Dictionary<string, string>();
            pairs.Add("type", "recognition");

            HttpFormUrlEncodedContent formContent = new HttpFormUrlEncodedContent(pairs);
            Windows.Web.Http.HttpResponseMessage response = await client.PostAsync(uri, formContent);

            string content = await response.Content.ReadAsStringAsync();
            if (response.IsSuccessStatusCode)
            {
            }
        }
        catch (Exception eu)
        {

        }
    }

If you are a question or need more information please tell me. 如果您有任何疑问或需要更多信息,请告诉我。

Thanks for your time. 谢谢你的时间。

First of all you have to read image file from storage and convert bytes to base64 string. 首先,您必须从存储读取图像文件并将字节转换为base64字符串。

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(PHOTO_PATH));

        byte[] rawBytes;
        using (Stream stream = await file.OpenStreamForReadAsync())
        {
            rawBytes = new byte[stream.Length];
            await stream.ReadAsync(rawBytes, 0, rawBytes.Length);
        }

        string base64Content = Convert.ToBase64String(rawBytes);

Then you have to make request with that content. 然后,您必须使用该内容进行请求。 I'm not sure how yours server accept requests but here is example of sending request with that string in content. 我不确定您的服务器如何接受请求,但这是发送包含该字符串内容的请求的示例。

var httpClient = new Windows.Web.Http.HttpClient();
        IBuffer content = CryptographicBuffer.ConvertStringToBinary(base64Content, BinaryStringEncoding.Utf8);

        var request = new HttpBufferContent(content);

        HttpResponseMessage response = await httpClient.PostAsync(new Uri(SERVER_URL), request);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM