I need to read a PDF document that I have stored in a folder within my project, I have the following code but it does not do anything, I can not find the error:
protected void btnConfirmar_Click(object sender, EventArgs e)
{
bool a = File.Exists(@"C:\Proyectos\NuevoProyecto\MiProyecto\ModuloGeneral\ImpresionConf\pdfFile.pdf");
string strLocalFilePath = @"C:\Proyectos\NuevoProyecto\MiProyecto\ModuloGeneral\ImpresionConf\pdfFile.pdf";
string fileName = "pdfFile.pdf";
Response.Clear();
Stream iStream = null;
const int bufferSize = 64 * 1024;
byte[] buffer = new Byte[bufferSize];
int length;
long dataToRead;
try
{
iStream = new FileStream(
strLocalFilePath,
FileMode.Open,
FileAccess.Read,
FileShare.Read);
dataToRead = iStream.Length; //--> iStream.Length = 24214
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
while (dataToRead > 0)
{
if (Response.IsClientConnected)
{
length = iStream.Read(buffer, 0, bufferSize);
Response.OutputStream.Write(buffer, 0, length);
Response.Flush();
buffer = new byte[bufferSize];
dataToRead = dataToRead - length;
}
else
{
//prevent infinate loop on disconnect
dataToRead = -1;
}
}
}
catch (Exception ex)
{
log.Error("btnConfirmar_Click", ex);
this.lblMensaje.Text = ex.Message.ToString();
}
finally
{
if (iStream != null)
{
iStream.Close();
}
}
}
From what I could see debugging is that an exception occurs in the Stream:
ReadTimeout = '((System.IO.Stream) (iStream)). ReadTimeout' produced an exception of type 'System.InvalidOperationException'
And also in the WriteTimeout property:
WriteTimeout = 'iStream.WriteTimeout' produced an exception of type 'System.InvalidOperationException'