将PDF流复制到文件

时间:2013-04-18 12:02:25

标签: c# php filestream tcpdf streamwriter

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

这是我用来执行请求的类(感谢用户使用'Paramiliar'作为我正在使用/借用/偷走的示例):

    public class httpPostData
    {
        WebRequest request;
        WebResponse response;

        public string senddata(string url, string postdata)
        {

            // create the request to the url passed in the paramaters
            request = (WebRequest)WebRequest.Create(url);


            // set the method to POST
            request.Method = "POST";

            // set the content type and the content length
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postdata.Length;

            // convert the post data into a byte array
            byte[] byteData = Encoding.UTF8.GetBytes(postdata);

            // get the request stream and write the data to it
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteData, 0, byteData.Length);
            dataStream.Close();

            // get the response
            response = request.GetResponse();
            dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);

            // read the response
            string serverresponse = reader.ReadToEnd();

            //Console.WriteLine(serverresponse);
            reader.Close();
            dataStream.Close();
            response.Close();

            return serverresponse;
        }
    } // end class httpPostData

......我的呼唤

httpPostData myPost = new httpPostData();    
// postData defined (not shown)
string response = myPost.senddata("http://www.example.com/pdf.php", postData);

如果不清楚,我会把string response写入有效的.pdf文件。我试过这个(感谢用户Adrian):

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

我认为我很接近......对于正确方向的推动会非常感激。

1 个答案:

答案 0 :(得分:0)

您可以使用WebClient。使用方法DownloadFile或异步方法。

玩得开心! 费尔南多.-


抱歉,到目前为止我还没看过你的评论。 我想你已经做过了......

但这可能对您有帮助(只需更换网址和路径)(来自:http://msdn.microsoft.com/en-us/library/ez801hhe.aspx

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