Search code examples
c#mime-typesmime

save file with MIME header to file


I must be missing something. I am downloading a file and want to save it as its type. I know this is supposed to end up as jpg, in this case.

When I save the byte array to a file I get the following:

--145ae7dc-e075-478c-8063-eeb765e0e15a
Content-Type: image/jpeg;
Content-Length: 41946
MIME-Version: 1.0

ÿØÿî Adobe d     ÿÃ   R G B ÿÄ             ÿÚ R G B   úÿ ×þ¿Úív¡

Code:

WebClient webClient = new WebClient();
webClient.Headers["Accept"] = textBoxMimeType.Text;

var uri = new Uri(url);
using (Stream webStream = webClient.OpenRead(uri))
{
    using (FileStream fileStream = new FileStream(outputFile, FileMode.Create))
    {
        var buffer = new byte[32768];
        int bytesRead;
        Int64 bytesReadComplete = 0;
        Int64 bytesTotal = Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]);

        while ((bytesRead = webStream.Read(buffer, 0, buffer.Length)) > 0)
        {
             bytesReadComplete += bytesRead;
             fileStream.Write(buffer, 0, bytesRead);
        }
     }
}

I use the above because I may have a very large file.

Another file comes across as "application/octet-stream"...

I want be able to download other mime types as well.

How do I get rid of the MIME header and save the binary data?

I have tried MimeKit and it fails to parse the body.

Thx


Solution

  • Well, I was hoping for a utility (I think chilkat actually does it for common files like jpg but I wasn't going to spend the money for a tool).

    It comes down to "magic numbers". Loading a buffer then searching for the "magic numbers" at the begin and ending (careful, I believe jpg can have different starting numbers).

    private void GetParseMime()
    {
        byte[] beginPattern = new byte[0];
        byte[] endPattern = new byte[0];
    
        WebClient webClient = new WebClient();
        webClient.Headers["Accept"] = textBoxMimeType.Text;
    
        bool bBeginSeqFound = false;
        long savedBytesRead = 0;
    
        var uri = new Uri(url);
        using (Stream webStream = webClient.OpenRead(uri))
        {
            long finalContentLen = long.Parse(webClient.ResponseHeaders["Content-Length"]);
    
            using (FileStream fileStream = new FileStream(outputFilename, FileMode.Create))
            {
                var buffer = new byte[32768];
                int bytesRead;
                long bytesTotal = Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]);
    
                while ((bytesRead = webStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    int len = bytesRead;
    
                    //look for the start sequence
                    if (!bBeginSeqFound)
                    {
                        int i = IndexOfSequence(buffer, dcmBeginPattern, 0);
                        if (i > -1)
                        {
                            bBeginSeqFound = true;
                            buffer = buffer.Skip(i).Take(bytesRead - i).ToArray();
                            len = bytesRead - i;
                        }
                    }
                    else
                    {
                        //look for end sequence
                        int i = IndexOfSequence(buffer, dcmEndPattern, 0);
                        if (i > -1)
                        {
                            buffer = buffer.Skip(0).Take(i).ToArray();
                            len = buffer.Length;
                        }
                    }
    
                    savedBytesRead += len;
                    fileStream.Write(buffer, 0, len);
                }
            }
        }
    }
    

    This is just the beginning. There are probably many problems with this. Doesn't handle multiple files within...

    Gina