将PDF流复制到文件



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

这是我用来执行请求的类(感谢用户'Paramiliar'的例子,我使用/borrow/stolen):

    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

我想我接近了…如果能朝正确的方向推动一下,我将不胜感激。

您可以使用WebClient。使用DownloadFile方法,或者async方法。

玩得开心!Fernando.——


对不起,我现在才看到你的评论。我想你已经这样做了……

但这可能会帮助你(只需替换url和路径)(from: 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}" .......nn", 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:nt" + Application.StartupPath);

相关内容

  • 没有找到相关文章

最新更新