Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

How to send a Json object (not in url) and an image to Web Api Service from Windows 8 App

$
0
0

How to send a Json object and an image to Web Api Service from Windows 8 Apps.

I don’t want to send as a parameters in url query string since the data might be too huge in my case. So I need to send an json object and image from win 8 apps to web api service.

Below is the controller and win 8 app coding which is working correctly for sending an image only.

Controller

async public Task<HttpResponseMessage> PostVideo()
        {
            if (ModelState.IsValid)
            {
                if (!Request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }

                string fileSaveLocation = HttpContext.Current.Server.MapPath("~/Images");
                CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation);

                try
                {
                    await Request.Content.ReadAsMultipartAsync(provider);
                    return Request.CreateResponse(HttpStatusCode.OK);
                }
                catch { }
            }

            return Request.CreateResponse(HttpStatusCode.InternalServerError);
        }


class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
    {
        public CustomMultipartFormDataStreamProvider(string path) : base(path)
        { }

        public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
        {
            var name = !string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName) ? headers.ContentDisposition.FileName : "NoName";
            return name.Replace("\"", string.Empty); //this is here because Chrome submits files in quotation marks which get treated as part of the filename and get escaped
        }
    }

Windows 8 App C#

async private Task<bool> PostImage()
        {
            try
            {
                StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Images/7.gif"));
                IRandomAccessStreamWithContentType randomAccessStream = await storageFile.OpenReadAsync();
                Stream stream = randomAccessStream.AsStreamForRead();

                MultipartFormDataContent content = new MultipartFormDataContent();
                content.Add(new StreamContent(stream), "media", storageFile.Name);

                // Post Method Call

                HttpClient http = new HttpClient();
                HttpResponseMessage response = await http.PostAsync(“http://localhost:60736/api/Videos/”, content);

                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                    await new MessageDialog("Failed to Create the Data 2 !!").ShowAsync();
            }
            catch { }
            return true;
        }

Below is my class which I can convert into json. But how do I send both simultaneously.  What are the necessary changes that I should do in Controller and windows 8 app.

public class Video
    {
        public virtual int Id { get; set; }
        public virtual string Title { get; set; }
        public virtual int Length { get; set; }
    }

Thanks in advance...

 



Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>