E
eventuranza
For my application, there users can upload and download files to the a
webserver. Straightforward enough. However, when they upload a PDF
file then try to download it, the file seems to be corrupted. The had
not been a problem before but it seems to have been introduced when I
refactored the writing to the response to a buffered version to prevent
potential OutOfMemoryExceptions (you wouldn't believe the size of some
of these files...).
Here's the code (pretty much jacked from a Microsoft knowledge base
article):
private void WriteToResponse(string filepath, string contentType,
NameValueCollection extraHeaders)
{
Stream iStream = null;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];
// Length of the file:
int length;
// Total bytes to read:
long dataToRead;
// Identify the file name.
string filename = Path.GetFileName(filepath);
try
{
// Open the file.
iStream = new FileStream(filepath, FileMode.Open,
FileAccess.Read, FileShare.Read);
// Total bytes to read:
dataToRead = iStream.Length;
Response.ContentType = contentType;
if(extraHeaders != null)
{
for (int i = 0; i < extraHeaders.Count; i++)
{
Trace.Write("adding Key value: " +
extraHeaders.GetKey(i)
+ ". Adding value: " +
extraHeaders);
Response.AddHeader(extraHeaders.GetKey(i),
extraHeaders);
}
}
// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch(IOException ioEx)
{
errorRow.Visible = true;
lblErrorMsg.Text = ioEx.Message;
log.Error(ioEx.ToString());
}
catch (Exception ex)
{
errorRow.Visible = true;
// Trap the error, if any.
log.Error(ex.ToString());
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
}
}
Thanks in advance for any help.
webserver. Straightforward enough. However, when they upload a PDF
file then try to download it, the file seems to be corrupted. The had
not been a problem before but it seems to have been introduced when I
refactored the writing to the response to a buffered version to prevent
potential OutOfMemoryExceptions (you wouldn't believe the size of some
of these files...).
Here's the code (pretty much jacked from a Microsoft knowledge base
article):
private void WriteToResponse(string filepath, string contentType,
NameValueCollection extraHeaders)
{
Stream iStream = null;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];
// Length of the file:
int length;
// Total bytes to read:
long dataToRead;
// Identify the file name.
string filename = Path.GetFileName(filepath);
try
{
// Open the file.
iStream = new FileStream(filepath, FileMode.Open,
FileAccess.Read, FileShare.Read);
// Total bytes to read:
dataToRead = iStream.Length;
Response.ContentType = contentType;
if(extraHeaders != null)
{
for (int i = 0; i < extraHeaders.Count; i++)
{
Trace.Write("adding Key value: " +
extraHeaders.GetKey(i)
+ ". Adding value: " +
extraHeaders);
Response.AddHeader(extraHeaders.GetKey(i),
extraHeaders);
}
}
// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch(IOException ioEx)
{
errorRow.Visible = true;
lblErrorMsg.Text = ioEx.Message;
log.Error(ioEx.ToString());
}
catch (Exception ex)
{
errorRow.Visible = true;
// Trap the error, if any.
log.Error(ex.ToString());
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
}
}
Thanks in advance for any help.