C# 将PDF流复制到文件

C# 将PDF流复制到文件,c#,php,filestream,tcpdf,streamwriter,C#,Php,Filestream,Tcpdf,Streamwriter,我正在使用StreamReader通过WebRequest从C#调用一个PHP例程(TCPDF)。PDF文件作为流返回并存储在字符串(obv)中。我知道返回到字符串的数据实际上是一个PDF文件,正如我在PHP中测试的那样。我很难将字符串写入一个文件,并且实际上得到了一个C#格式的有效PDF。我知道这与我试图对文件进行编码的方式有关,但我尝试过的几件事导致了“今天不行,神父”(即它们不起作用) 下面是我用来执行请求的类(感谢用户“Paramiliar”,我正在使用/借用/窃取的示例): …还有我的

我正在使用StreamReader通过WebRequest从C#调用一个PHP例程(TCPDF)。PDF文件作为流返回并存储在字符串(obv)中。我知道返回到字符串的数据实际上是一个PDF文件,正如我在PHP中测试的那样。我很难将字符串写入一个文件,并且实际上得到了一个C#格式的有效PDF。我知道这与我试图对文件进行编码的方式有关,但我尝试过的几件事导致了“今天不行,神父”(即它们不起作用)

下面是我用来执行请求的类(感谢用户“Paramiliar”,我正在使用/借用/窃取的示例):

…还有我的号召

httpPostData myPost = new httpPostData();    
// postData defined (not shown)
string response = myPost.senddata("http://www.example.com/pdf.php", postData);
如果不清楚,我将无法将
字符串响应
写入有效的.pdf文件。我尝试过这个(感谢用户Adrian):

static public void SaveStreamToFile(字符串fileFullPath,Stream-Stream)
{
如果(stream.Length==0)返回;
//创建FileStream对象以将流写入文件
使用(FileStream FileStream=System.IO.File.Create(fileFullPath,(int)stream.Length))
{
//用流数据填充bytes[]数组
byte[]ByteInstream=新字节[stream.Length];
读取(字节流,0,(int)字节流长度);
//使用FileStream对象写入指定的文件
Write(bytesInStream,0,bytesInStream.Length);
}
}
…以及对它的呼吁:
字符串位置=“C:\\myLocation\\”;
SaveStreamToFile(位置、响应);// 你可以用。使用方法,或异步方法

玩得开心! 费尔南多-


对不起,我到现在还没有读到你的评论。 我想你已经这么做了

但这可能会帮助您(只需替换URL和路径)(from:)


@费尔南多:虽然我很感谢你的建议(我会看看WebClient),但你并没有真正回答这个问题。另外,正如您从我发布的示例中所看到的,没有文件被下载,只有一个流被捕获(从技术意义上讲,不会讨论为什么这可能是同一件事的语义)。我不想在服务器端创建文件-我想将POST请求创建的流下载到PHP脚本中。我不想将这个问题标记为“已回答”,因为我从来都不知道如何将.pdf流捕获为字符串,然后将该字符串写入有效的.pdf文件。我试图消除这一点:(oops)消除多个步骤(即在服务器上创建文件,从服务器下载文件,然后删除服务器上的文件)。似乎有三个不必要的步骤。最后,我就是这样处理的,因为我在解决方案上花费了太多的时间。我对这三个步骤的处理速度感到惊讶(
httpPostData myPost = new httpPostData();    
// postData defined (not shown)
string response = myPost.senddata("http://www.example.com/pdf.php", postData);
static public void SaveStreamToFile(string fileFullPath, Stream stream)
    {
        if (stream.Length == 0) return;

        // Create a FileStream object to write a stream to a file
        using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
        {
            // Fill the bytes[] array with the stream data
            byte[] bytesInStream = new byte[stream.Length];
            stream.Read(bytesInStream, 0, (int)bytesInStream.Length);

            // Use FileStream object to write to the specified file
            fileStream.Write(bytesInStream, 0, bytesInStream.Length);
        }
    }

..and the call to it:

    string location = "C:\\myLocation\\";

    SaveStreamToFile(location, response);  // <<-- this throws an error b/c 'response' is a string, not a stream. New to C# and having some basic issues with things like this
string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;

Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);

// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName);     

Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);

Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);