1

This question deals mainly with streams in web application in .net. In my webapplication I will display as follows:

  1. bottle.doc
  2. sheet.xls
  3. presentation.ppt
  4. stackof.jpg

    Button

I will keep checkbox for each one to select. Suppose a user selected the four files and clicked the button,which I kept under. Then I instantiate clasees for each type of file to convert into pdf, which I wrote already and converted them into pdf and return them. My problem is the clases is able to read the data form URL and convert them into pdf. But I don't know how to return the streams and merge them.

string url = @"url";

//Prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/mspowerpoint";
request.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";

//Execute the request
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

//We will read data via the response stream
Stream resStream = response.GetResponseStream();

//Write content into the MemoryStream
BinaryReader resReader = new BinaryReader(resStream);

MemoryStream PresentaionStream = new MemoryStream(resReader.ReadBytes((int)response.ContentLength));
//convert the presention stream into pdf and save it to local disk.

But I would like to return the stream again. How can I achieve this any Ideas are welcome.

Rob
  • 45,296
  • 24
  • 122
  • 150
Tortoise
  • 731
  • 4
  • 10
  • 21
  • It seems you have two questions. 1. How to merge many streams into one and convert this to a pdf. 2. How do I return a pdf stream to the client. I would keep number 2 here and ask a new question for number 1 as it is a completely different subject. – Peter Mar 03 '11 at 09:22

2 Answers2

1

If I understand your question correctly, you can immediately send it with the response, this way the user will get a downloadrequest.

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.AddHeader("Content-Type", "binary/octet-stream");
response.AddHeader("Content-Disposition", "attachment; filename=nameofthefile.pdf; size=" + downloadBytes.Length.ToString());
response.Flush();
response.BinaryWrite(downloadBytes);
response.Flush();
response.End();

where downloadBytes is a byte[] and contains the pdf.

Peter
  • 14,221
  • 15
  • 70
  • 110
  • I don't want to download , i would like to send the file directly to the printer in client side – Tortoise Mar 03 '11 at 09:01
  • In a web environment that would be impossible. How do you know if the user even has a printer? With my answer the user will receive a download request for the pdf, if he opens the file after download, he can print it from his pdf viewer. – Peter Mar 03 '11 at 09:18
  • No my application requirement is when the user should be prompted directly with a print dialogue without any intermediate steps.That is my requirement and I am doing a print application. – Tortoise Mar 03 '11 at 09:33
1

I'm assuming this is a asp.net page and you're fetching the pdf from a service. You don't need to save it locally before return it to the user. You can just write to the outputstream in chunks.

//Execute the request
HttpWebResponse response = null;
try
{
    response = (HttpWebResponse)request.GetResponse();
}
catch (WebException we) { // handle web excetpions }
catch (Exception e) { // handle other exceptions }

this.Response.ContentType = "application/pdf";

const int BUFFER_SIZE = 1024;
byte[] buffer = new byte[BUFFER_SIZE];
int bytes = 0;
while ((bytes = resStream.Read(buffer, 0, BUFFER_SIZE)) > 0)
{
    //Write the stream directly to the client 
    this.Response.OutputStream.Write(buff, 0, bytes);
}
Torbjörn Hansson
  • 18,354
  • 5
  • 33
  • 42
  • But here I am converting multiple files with one single request.So how can I differentiate between different streams. – Tortoise Mar 03 '11 at 09:01
  • Well we cannot send severals responses to the user, so the option is to zip them or let the pdf-service combine all files in one big pdf. So if you need to zip them you need to create a zip file and write the stream to the zip. See http://stackoverflow.com/questions/276319/create-zip-archive-from-multiple-in-memory-files-in-c/276347#276347 – Torbjörn Hansson Mar 03 '11 at 10:01