2

I have a Phonegap app and want to send audio files from the application to a WCF REST API. I went through stackoverflow and found a lot of questions with the same requirements.

best approach to design a rest web service with binary data to be consumed from the browser

How do I upload a file with metadata using a REST web service?

Posting a File and Associated Data to a RESTful WebService preferably as JSON

From these, I started using the method of sending the metadata first and then using the record identification and sending the file later. But I must be doing something wrong and cannot get this to work. I have posted my code samples below.

JS

var formData = new FormData('file://beep.wav');
$.ajax({
    type: "PUT",
    url: serviceURL + 'VoiceRecord/' + voicerecordId,
    data: formData,
    headers: {
        'token': securityToken
    },
    contentType: false,
    cache: false,
    processData: false,
    success: onSaveVoiceFileSuccess,
    error: function (xhr, status, error) {
        common.onAjaxError(xhr, status, error, 'wrapper, footer');
    },
    async: false
});

WCF

Contract

[WebInvoke(Method = "PUT",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "VoiceRecord/{voicerecordId}")]
[OperationContract]
void UpdateVoiceRecord(string voicerecordId, Stream stream);

Implementation

public void UpdateVoiceRecord(string voicerecordId, Stream stream)
    {
        try
        {
            log.Info("Update method called.");
            if (Utilities.ValidateToken())
            {
                log.Info("Security validated");
                using (StreamReader s = new StreamReader(stream))
                {
                    log.Info(s.ReadToEnd());
                }
                HttpMultipartParser parser = new HttpMultipartParser(stream, "boundary");
                log.Info("Parser created");

                if (parser.Success)
                {
                    log.Info("Parser success");
                    int result;
                    bool value = int.TryParse(voicerecordId, out result);
                    if (value)
                    {
                        var voicerecord = unitofwork.VoiceRecordRepository.GetByID(result);
                        if (voicerecord != null)
                        {
                            log.Info("Parser file contents" + parser.FileContents);
                            voicerecord.Record = parser.FileContents;
                            unitofwork.VoiceRecordRepository.Update(voicerecord);
                            unitofwork.Commit();
                        }
                    }
                }
            }
            else
            {
                WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized;
            }
        }
        catch (Exception x)
        {
            log.Error(x);
        }
    }

I am using the HttpFormParser to parse the incoming stream. But it is not parsed correctly. I intercepted the stream and only found this string as incoming.

-----------------------------7de23919108a8--

Any help would be appreciated.

0

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.