.net stream fast web view Linearized Pdf

Wednesday, July 9, 2014

I'm trying to stream large linearized PDFs one page at a time. I have the PDFs saved with "Fast Web View" I'm getting them with an async download from an SFTP server using SSH.Net, then stream then to the browser. When I try with the code below I get the progress bar that slowly loads to 100% and then I get the full document at once, rather than one page at a time while its downloading. Here is my relevant code:



using (var client = new SftpClient(sftpHost, port, username, password))
{
try
{
client.Connect();
client.ChangeDirectory(envDirectory);
httpResponse.Clear();
httpResponse.ClearContent();
httpResponse.ClearHeaders();
httpResponse.Buffer = true;
httpResponse.ContentType = "application/pdf";
long docSize = client.Get(fileNameWithExtension).Length;
ulong uDocSize = (ulong)docSize;
httpResponse.AddHeader("Content-Length", docSize.ToString());
httpResponse.AddHeader("Expires", "0");
httpResponse.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
httpResponse.AddHeader("Content-Disposition", "inline;filename=" + fileNameWithExtension);

httpResponse.AddHeader("Accept-Ranges", "bytes");


var dl = client.BeginDownloadFile(fileNameWithExtension, httpResponse.OutputStream);
var asynchDl = dl as Renci.SshNet.Sftp.SftpDownloadAsyncResult;
System.Diagnostics.Debug.WriteLine("Downloading");
while (!asynchDl.IsCompleted)
{
System.Diagnostics.Debug.WriteLine(asynchDl.DownloadedBytes + "/" + docSize);
httpResponse.Flush();
if (asynchDl.DownloadedBytes != uDocSize)
{
System.Threading.Thread.Sleep(2000);
}
}
httpResponse.Flush();
client.EndDownloadFile(asynchDl);
}


Any help or better ideas would be great.







http://ift.tt/1ojnQ2F