Click here to Skip to main content
15,887,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. i'm trying to POST audio file to my Web Service ( ASP.Net 4.5 Web API ) and get Response from server but have error. So please help me fix that

Here my code to POST Audio

C#
byte[] buffer;
        private async void Upload(StorageFile fileStream)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://sv.volcanosoft.com:47581/api/SpeechRecognition?dataType=json"));
            
            request.Method = "POST";
            request.ContentType = "multipart/form-data";
            request.UseDefaultCredentials = true;

            var file = await fileStream.OpenReadAsync();

            byte[] audioDataBytes;
            audioDataBytes = new byte[file.Size];
            for (int i = 0; i < audioDataBytes.Length; i++)
            {
                audioDataBytes[i] = (byte)file.AsStreamForRead().ReadByte();
            }

            const int MAX_URI_LENGTH = 32766;
            string base64audio = System.Convert.ToBase64String(audioDataBytes);
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < base64audio.Length; i += MAX_URI_LENGTH)
            {
                sb.Append(Uri.EscapeDataString(base64audio.Substring(i, Math.Min(MAX_URI_LENGTH, base64audio.Length - i))));
            }

            string uploadRequestString = sb.ToString();

            buffer = Encoding.UTF8.GetBytes(uploadRequestString);

            request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request);
        }

        private void ReadCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            // End the operation.            
            Stream postStream = request.EndGetRequestStream(asynchronousResult);
            postStream.Write(buffer, 0, buffer.Length);
            postStream.Close();
            request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
        }

        private void ResponseCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            Stream streamResponse = resp.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            string responseString = streamRead.ReadToEnd();
            Dispatcher.BeginInvoke(new Action(() => { MessageBox.Show(responseString); }));
            // Close the stream object.            
            streamResponse.Close();
            streamRead.Close();
            // Release the HttpWebResponse.            
            resp.Close();
        }


And here my error on Server

Invalid 'HttpContent' instance provided. It does not have a 'multipart' content-type header with a 'boundary' parameter.
Parameter name: content
Posted

The problem is in your server side code, you didn't post that so take a look at this post and see if you can figure out what is wrong with your server side code.
http://stackoverflow.com/questions/7460088/reading-file-input-from-a-multipart-form-data-post[^]
 
Share this answer
 
It have problem in WP8 only, but on W8 and Desktop App version working very well :(
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900