简体   繁体   中英

how to convert jpeg image to byte format

I am currently using Windows phone application to click a picture and I wanted to upload that image to a web service using HTTP post request. I don't want to use windows phone silverlight.

How can I send that image to the web service URL?

Posting image on http is just like posting any other file type. use following code snippet

public string PostFileUsingApi()
{
    string result = "";
    string param1 = "value1";

    using (var handler = new HttpClientHandler()) {
        using (var client = new HttpClient(handler) { BaseAddress = new Uri("http://localhost:8008") }) {
            client.Timeout = new TimeSpan(0, 20, 0);

            StorageFile storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
            Stream stream = await storageFile.OpenStreamForReadAsync();

            var requestContent = new MultipartFormDataContent();
            StreamContent fileContent = new StreamContent(stream);
            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") {
                Name = "imagekey", //content key goes here
                FileName = "myimage"
            };

            fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/bmp");

            requestContent.Add(fileContent);

            client.DefaultRequestHeaders.Add("ClientSecretKey", "ClientSecretValue");

            HttpResponseMessage response = await client.PostAsync("api/controller/UploadData?param1=" + HttpUtility.UrlEncode(param1), requestContent).Result;

            if (response.StatusCode == System.Net.HttpStatusCode.OK) {
                result = await response.Content.ReadAsStringAsync().Result
            } else {
                result = "";
            }
        }
    }

    return result;
}

install this package to resolve dependencies https://www.nuget.org/packages/microsoft.aspnet.webapi.client/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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