Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Base64编码字符串转换为PDF_C#_Asp.net Mvc_Utf 8 - Fatal编程技术网

C# 将Base64编码字符串转换为PDF

C# 将Base64编码字符串转换为PDF,c#,asp.net-mvc,utf-8,C#,Asp.net Mvc,Utf 8,我有Base64字符串,它表示需要转换为PDF文件的PDF文件,并使用默认的PDF阅读器或浏览器在C#中打开 我只写了Base64字符串的一部分,因为它太长了,无法粘贴到这里 public void DownloadPDF() { string pdflocation = "E:\\"; string fileName = "softcore.pdf"; // This is only part of Base64 string var base64String

我有Base64字符串,它表示需要转换为PDF文件的PDF文件,并使用默认的PDF阅读器或浏览器在C#中打开

我只写了Base64字符串的一部分,因为它太长了,无法粘贴到这里

public void DownloadPDF()
{
    string pdflocation = "E:\\";
    string fileName = "softcore.pdf";
    // This is only part of Base64 string
    var base64String = "JVBERi0xLjQKJdP0zOEKMSAwIue704O58dOXPgst+hmQ+laj/"; 
    int mod4 = base64String.Length % 4;
    if (mod4 > 0)
    {
        base64String += new string('=', 4 - mod4);
    }
    byte[] data = Convert.FromBase64String(base64String);        

    if (Directory.Exists(pdflocation))
    {
        pdflocation = pdflocation + fileName;
        using (MemoryStream Writer = new System.IO.MemoryStream())
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
            Response.AddHeader("content-length", data.Length.ToString());
            Writer.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();
            //Writer.Write(data, 0, data.Length);
        }                
    }
}
我面临的问题是,它显示为pdf生成,但最后显示为网络错误

输入编码的Base64字符串中的解码字符串:

如果PDF是从某个在线平台(如freeformatter.com/base64 encoder.html)转换为base 64,并在下面的代码中使用该base 64字符串,则此代码可以正常工作:

                 string pdflocation = "D:\\";
                  string fileName = "softcore.pdf";

                  // put your base64 string converted from online platform here instead of V
                  var base64String = V;

                  int mod4 = base64String.Length % 4;

                  // as of my research this mod4 will be greater than 0 if the base 64 string is corrupted
                  if (mod4 > 0)
                  {
                        base64String += new string('=', 4 - mod4);
                  }
                  pdflocation = pdflocation + fileName;

                  byte[] data = Convert.FromBase64String(base64String);

                  using (FileStream stream = System.IO.File.Create(pdflocation))
                  {
                        stream.Write(data, 0, data.Length);
                  }
这应该将PDF文件保存在
D:\\
中,问题再次出现在base64编码字符串中


希望这能有所帮助。

请编辑您的问题,将代码显示为文本而不是图像,并告诉我们目前出现了什么问题。您已经显示了一些代码,但没有告诉我们您面临的问题。@DaisyShipton问题是,它显示为pdf生成,但最后显示为网络错误。请将这些信息放入您的问题中(更详细-什么是“网络错误”?),然后编辑您的问题,将代码作为文本包含在内。顺便说一句,你的捕获块会丢失信息。。。我强烈建议完全删除try/catch。@DaisyShipton:我没有粘贴代码,因为Base64字符串太长,无法粘贴到这里。因此,请显示一个较短的字符串,并添加一条注释,说明您实际上是从其他地方获得它的。(我假设您没有将其硬编码到生产代码中。)显示代码的图像并不是解决该问题的最佳方法。是的,我认为base64字符串可能已损坏。我需要和客户确认一下。无论如何,谢谢你的回复。