1

My object will look like below:

var object = {User:{UserName:'test',UserId:232},
Addresses:[
    Address:{City:'Chennai',State:'Tamilnadu',PostCode:'34344'},
    Address:{City:'Salem',State:'Tamilnadu',PostCode:'243254'
    ]};

along with this object, I want to pass file which may contain image/audio/video/document.

Now I want to pass this from MVC controller to API. I have converted file as Streamcontent and pass this with object as Json as below.

var fileContent = Request.Files[file];
                if (fileContent != null && fileContent.ContentLength > 0)
                {
                    stream = new StreamContent(fileContent.InputStream);
                    stream.Headers.Add("Content-Type", "application/octet-stream");
                    stream.Headers.Add("Content-Disposition", "form-data; name=\"file\"; filename=\"" + fileContent.FileName + "\"");

                }
     using (var response = client.PostAsJsonAsync(url, new { stream, Addresses, User }).Result)

Now this will pass the objects and Stream as Json to API. I retrieve the objects in API as FromBody

I can able to deserialize the User and Address objects from Json But I can't able to deserialize the stream.

any one have idea to convert the Json to stream or is there any other way to pass the complex object like above with file?

EDIT: my actual issue is that, i want to send complex object (it will look like above) with File (it may contain image/audio/video/document) from MVC controller to API in C#

Thanks

0 Answers0