简体   繁体   中英

c# how to save IO.Stream to MemoryStream and save to PDF file on local folder

I am calling a third party service, which return a pdf file by IO.Stream. I need to change to MemoryStream, and save to a pdf file.

You could do the following.

using (Stream stream = response.GetResponseStream()) 
using(MemoryStream memStream = new MemoryStream())
{
  memStream = new MemoryStream();
  stream.CopyTo(memoryStream);
  // TODO : Rest of your task
}

More details on Stream.CopyTo on MSDN

cRequestString = ".....";//You need to set up you own URL here.  
            //Make the API call  
            try  
            {  
    byte[] bHeaderBytes = System.Text.Encoding.UTF8.GetBytes(GetUserPasswordString()); //user and pa for third party call.  
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(cRequestString);  
                request.Method = WebRequestMethods.Http.Get;  
                request.PreAuthenticate = true;  
                request.ContentType = "application/pdf";  
                request.Accept = "application/pdf";  
                request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(bHeaderBytes));  
                MemoryStream memStream;   
                WebResponse response = request.GetResponse();                 
                using (Stream stream = response.GetResponseStream())                   
                using (StreamReader reader = new StreamReader(stream))  
                {  
                    memStream = new MemoryStream();  
                    //read small blocks to show correctly  
                    byte[] buffer = new Byte[1023];  
                    int byteCount;  
                    do  
                    {  
                        byteCount = stream.Read(buffer, 0, buffer.Length);  
                        memStream.Write(buffer, 0, byteCount);  
                    } while (byteCount > 0);  
                }  
                memStream.Seek(0, SeekOrigin.Begin);//set position to beginning    
                return memStream;    
            }  
            catch  
            {  
                return null;  
            }  

//save MemoryStream to local pdf file  
 private void SavePDFFile(string cReportName, MemoryStream  pdfStream)  
        {  
            //Check file exists, delete  
            if (File.Exists(cReportName))  
            {  
                File.Delete(cReportName);  
            }  
            using (FileStream file = new FileStream(cReportName, FileMode.Create, FileAccess.Write))  
            {  
                byte[] bytes = new byte[pdfStream.Length];  
                pdfStream.Read(bytes, 0, (int)pdfStream.Length);  
                file.Write(bytes, 0, bytes.Length);  
                pdfStream.Close();  
            }  
        }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM