I am accessing a httpwebrequest that returns a pdf file as the response. I am reading that response into a memory stream, and later on converting to a file. The problem is, is that hundreds of files are being created. Not sure why, I've tried many ways, and all do the same... This is the first method which returns the memorystream.
MemoryStream memStream = new MemoryStream();
byte[] buffer = new byte[2048];
int bytesRead = 0;
do
{
bytesRead = _ResponseStream.Read(buffer, 0, buffer.Length);
memStream.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
Second to convert to FileStream...
byte[] data = stream.ToArray();
FileStream fs = new FileStream(newFile, FileMode.CreateNew);
fs.Write(data, 0, data.Length);
Any ideas?
EDIT TO ADD MORE CODE...
public MemoryStream GetToStream()
{
if (_Req == null)
throw new Exception("HttpWebRequest is not initialised");
GetResult(_Req);
MemoryStream memStream = new MemoryStream();
byte[] buffer = new byte[2048];
int bytesRead = 0;
do
{
bytesRead = _ResponseStream.Read(buffer, 0, buffer.Length);
memStream.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
_ResponseStream.Close();
if (memStream.Length > 0)
return memStream;
else return null;
}
newFile is the mapped path to the server of the file to create.
byte[] data = stream.ToArray();
FileStream fs = new FileStream(newFile, FileMode.Create);
fs.Write(data, 0, data.Length);
fs.Close();
I've tried stream.WriteTo(fs), and same thing occurs. Quite bizzare.