locked
how to determine mime type of multi-part message uploaded to rest api? RRS feed

  • Question

  • I have a rest api that is used to upload data from different types of clients and serves as a proxy to write data to Azure blob storage. For testing, I'm using Fiddler on a PC.

    The code in my rest api that writes to blob storage looks like this:

                    try
                    {
                        await blockBlob.UploadFromStreamAsync(requestMessage.Content.ReadAsStreamAsync().Result);
                    }
                    catch (Exception ex)
                    {
                        HttpResponseMessage _hrm = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                        _hrm.ReasonPhrase = ex.Message;
                        throw new HttpResponseException(_hrm);
                    }
    

    1 - How can I access the form-data that is included in the POST that has the original file name and the content-tyipe?

    2 - How can this info be passed to the blob when I write it out?  Currently, it is written out as a type of application/octet-stream instead of video/mp4.

    Wednesday, April 8, 2015 3:58 PM

Answers

All replies

  • I think I found a solution using some other threads.  I'm not sure if it's the most performant or best but I'm able to get to the content-disposition and the content-type of the stream to get the details:

                    Dictionary<string, string> lstLines = new Dictionary<string, string>();
                    string[] _tmp;
                    TextReader textReader = new StreamReader(requestMessage.Content.ReadAsStreamAsync().Result);
                    string sLine = textReader.ReadLine();
                    Regex regex = new Regex("(^content-type)|(^content-disposition)", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
    
                    while (sLine != ""
                        && sLine != null)
                    {
                        if (regex.Match(sLine).Success)
                        {
                           _tmp = sLine.Split(':');
                           lstLines.Add(_tmp[0], _tmp[1]);
                        }
                        sLine = textReader.ReadLine();
                    }
    

    Wednesday, April 8, 2015 10:50 PM
    • Marked as answer by bmobile Thursday, April 9, 2015 5:33 AM
    Thursday, April 9, 2015 5:33 AM
  • Thank you for sharing the solution with us.
    It definitely would be helpful if someone is facing a similar issue.

    Regards,
    Malar.

    Thursday, April 9, 2015 8:05 AM