开发者

Combining the streams:Web application

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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜