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

6
  • That's not valid json. If you wrap it in {} it would be... Commented Mar 17, 2017 at 11:48
  • 1
    Possible duplicate of Creating json object in mvc and returning from controller Commented Mar 17, 2017 at 11:52
  • no. that is not json.. i just provide example of my object. Commented Mar 17, 2017 at 11:53
  • It's not an object either. It's invalid.. Commented Mar 17, 2017 at 11:54
  • Sorry.. My object contains UserId,Username and list of Address and HttpPostedFile (it may be image/audio/video/document). I want to pass this from MVC controller to API Commented Mar 17, 2017 at 11:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.