【问题标题】:save file with MIME header to file将带有 MIME 标头的文件保存到文件
【发布时间】:2019-03-20 10:46:33
【问题描述】:

我一定错过了什么。我正在下载一个文件并想将其保存为它的类型。我知道这应该以 jpg 结尾,在这种情况下。

当我将字节数组保存到文件中时,我得到以下信息:

--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¡

代码:

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);
        }
     }
}

我使用上面的,因为我可能有一个非常大的文件。

另一个文件是“application/octet-stream”...

我也希望能够下载其他 mime 类型。

如何去掉 MIME 标头并保存二进制数据?

我尝试过 MimeKit,但它无法解析正文。

谢谢

【问题讨论】:

    标签: c# mime-types mime


    【解决方案1】:

    您可以用一种方法替换这些行:

    webClient.DownloadFile(url, outputFile);

    【讨论】:

    • 不能解决问题。它是一个带有 MIME 页眉/页脚的文件。我需要解析 MIME 正文。
    • 如果还是不行,是不是header不是http header而是自定义header?
    【解决方案2】:

    嗯,我希望有一个实用程序(我认为 chilkat 实际上是为 jpg 等常见文件做的,但我不打算花钱买一个工具)。

    归结为“幻数”。加载缓冲区,然后在开头和结尾搜索“幻数”(小心,我相信 jpg 可以有不同的起始数字)。

    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);
                }
            }
        }
    }
    

    这只是开始。这可能有很多问题。不处理...内的多个文件

    吉娜

    【讨论】:

      猜你喜欢
      • 2011-12-20
      • 1970-01-01
      • 2012-01-19
      • 2016-12-07
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      相关资源
      最近更新 更多