returning multipart content into javascript text and audio file

returning multipart content into javascript text and audio file from a webservice... How do I get both in javascript?
cbruneAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kblauCommented:
Here is what I have.  I give credit to proper people on the internet.
Create an WebAPI project.
public class HomeController : ApiController
    {
        public IEnumerable<string> Get()
        {
            return new string[] { "gh1", "gh2" };


        }

        public string Get(int id)
        {
            return "gh3";


        }

        public string Post() { return "ap"; }

        public void Put(int id, string value) { }

        public void Delete(int id) { }
    }
--------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;

namespace eeMultipPartGH3
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            var batchHandler = new BatchHandler(config);

            config.Routes.MapHttpRoute("batch", "api/batch",
                                        null, null, batchHandler);
            config.Routes.MapHttpRoute("Default", "api/{controller}/{id}",
                                        new { id = RouteParameter.Optional });

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

    public class BatchHandler : HttpMessageHandler
    {
        HttpMessageInvoker _server;
        public BatchHandler(HttpConfiguration config)
        {
            _server = new HttpMessageInvoker(new HttpServer(config));
        }
        protected override async Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            // Return 400 for the wrong MIME type      
            if ("multipart/batch" != request.Content.Headers.ContentType.MediaType)
            {
                return request.CreateResponse(HttpStatusCode.BadRequest);
            }
            // Start a multipart response        
            var outerContent = new MultipartContent("batch");
            var outerResp = request.CreateResponse();
            outerResp.Content = outerContent;
            // Read the multipart request    
            var multipart = await request.Content.ReadAsMultipartAsync();
            foreach (var httpContent in multipart.Contents)
            {
                HttpResponseMessage innerResp = null;
                try
                {
                    // Decode the request object    
                    var innerReq = await
                        httpContent.ReadAsHttpRequestMessageAsync();
                    // Send the request through the pipeline    
                    innerResp = await _server.SendAsync(
                        innerReq, cancellationToken);
                }
                catch (Exception)
                {
                    // If exceptions are thrown, send back generic 400    
                    innerResp = new HttpResponseMessage(
                        HttpStatusCode.BadRequest);
                }             // Wrap the response in a message content and put it      
                // into the multipart response          
                outerContent.Add(new HttpMessageContent(innerResp));
            } return outerResp;
        }
    }
}
-----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace eeMultipPartGH3
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //routes.MapRoute(
            //    name: "DefaultOriginal",
            //    url: "{controller}/{action}/{id}",
            //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

            routes.MapRoute(
                name: "DefaultOriginal",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Post", id = UrlParameter.Optional }
            );
           
        }
    }
}
----
client:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            var client = new HttpClient();
            var batchRequest = new HttpRequestMessage(
                HttpMethod.Post, "http://localhost:55187/api/batch");
            var batchContent = new MultipartContent("batch");
            batchRequest.Content = batchContent;
            batchContent.Add(new HttpMessageContent
                (new HttpRequestMessage(HttpMethod.Get,
                    "http://localhost:55187/api/values")));
            batchContent.Add(
                new HttpMessageContent(
                    new HttpRequestMessage(HttpMethod.Get,
                        "http://localhost:55187/foo/bar")));
            batchContent.Add(new HttpMessageContent(
                new HttpRequestMessage(HttpMethod.Get,
                    "http://localhost:55187/api/values/1")));

            using (Stream stdout = Console.OpenStandardOutput())
            {
                Console.WriteLine("<<< REQUEST >>>");
                Console.WriteLine();
                Console.WriteLine(batchRequest);
                Console.WriteLine();
                batchContent.CopyToAsync(stdout).Wait();
                Console.WriteLine();
                var batchResponse = client.SendAsync(batchRequest).Result;
                Console.WriteLine("<<< RESPONSE >>>");
                Console.WriteLine();
                Console.WriteLine(batchResponse);
                Console.WriteLine();
                batchResponse.Content.CopyToAsync(stdout).Wait();
                Console.WriteLine(); Console.WriteLine();
            }
        }
    }
}

add nuget package using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            var client = new HttpClient();
            var batchRequest = new HttpRequestMessage(
                HttpMethod.Post, "http://localhost:55187/api/batch");
            var batchContent = new MultipartContent("batch");
            batchRequest.Content = batchContent;
            batchContent.Add(new HttpMessageContent
                (new HttpRequestMessage(HttpMethod.Get,
                    "http://localhost:55187/api/values")));
            batchContent.Add(
                new HttpMessageContent(
                    new HttpRequestMessage(HttpMethod.Get,
                        "http://localhost:55187/foo/bar")));
            batchContent.Add(new HttpMessageContent(
                new HttpRequestMessage(HttpMethod.Get,
                    "http://localhost:55187/api/values/1")));

            using (Stream stdout = Console.OpenStandardOutput())
            {
                Console.WriteLine("<<< REQUEST >>>");
                Console.WriteLine();
                Console.WriteLine(batchRequest);
                Console.WriteLine();
                batchContent.CopyToAsync(stdout).Wait();
                Console.WriteLine();
                var batchResponse = client.SendAsync(batchRequest).Result;
                Console.WriteLine("<<< RESPONSE >>>");
                Console.WriteLine();
                Console.WriteLine(batchResponse);
                Console.WriteLine();
                batchResponse.Content.CopyToAsync(stdout).Wait();
                Console.WriteLine(); Console.WriteLine();
            }
        }
    }
}

-----add nuget package System.Net.Http.Formatting version  keep searching some of this package don't work you need exact,
version something like 4.0.21112.0 or v4.0.30319 , only right one will compile
--
so call the above , some of it will fail, but what works , wiill shoow how multipart can be in request! (unique). so call this webservice/webapi stright from jquery ie. the following:
this will make it so you have the boundary pieces in javascript:
for example:
<script type="text/javascript">
        $(document).ready(function () {
            $body = $("body");
            $(document).on({
                ajaxStart: function () { $body.addClass("loading"); },
                ajaxStop: function () { $body.removeClass("loading"); }
            });

            $("#callWSO2").click(function (event) {
                event.preventDefault();
                var wso2 = {
                    Request: 'dh',
                    Response:'ap'
                }
                $.ajax({
                    //url: '/WSO2Admin/CallWSO2',
                    url: '/WebService1.asmx/AP',
                    type: 'POST',
                    data: {},
                    contentType: "application/json; charset=utf-8",
                    //contentType: "multipart/form-data",

                    //mimeType: "multipart/form-data",
                    dataType: "json",
                    success: function (result) {
                        alert(result.d.Response);
                        //$("#idTextAreaResponse").css('color', 'black');
                       
                        //$("#idTextAreaResponse").val(result.d.Response);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }

                });
            });

        });

        function beforeSubmit() {
            $("#idTextAreaFor").text($("#idTextAreaFor").html());
            $("#idTextAreaResponse").text('Please wait..');
            $("#idTextAreaResponse").css('color','red');
        }
    </script>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.