G
Guest
I've spent the last 6 months developing a pay-per-download website using
ASP.NET
Users purchase documents and then download them.
The intention is that users are only charged for documents they successfuly
download.
My problem revolves around detecting a successful download, the steps I take
to handle the download are as follows:
Chunk the file into 1024byte chunks
Stream each chunk out to the clients browser
After each chunk is sent check if the client is still connected
One all chunks have been streamed out check the client is still connected,
if the client IS still connected then I deam the download successful.
-Code-
/// <summary>
/// Stream the file held in the MemoryStream object out to the client
/// </summary>
/// <param name="file">A MemoryStream containing the file to be streamed
to the client</param>
/// <param name="fileName">A string with the name of the file to be
streamed to the client</param>
/// <returns>A boolean indicating if the stream was successful or
not</returns>
private bool StreamFile(MemoryStream file, string fileName)
{
// reset the position in the file to the start
file.Position = 0;
// Size of the file chunks in bytes
int chunkSize = 1024;
// Buffer to read 1K bytes in chunk:
byte[] buffer = new Byte[chunkSize];
// Length of the buffer content:
int length;
// Total bytes to read:
long dataToRead;
bool success = false;
try
{
// total bytes to read
dataToRead = file.Length;
// Clear the response and add header content
Response.BufferOutput=false;
Response.Buffer=false;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-length", dataToRead.ToString());
Response.AddHeader("Content-Disposition","attachment; filename =" +
fileName);
Response.Flush();
// Write the file out to the client in fileChunkSize pieces
// checking that the client is still connected each time
while(dataToRead > 0 && Response.IsClientConnected)
{
// Read the data in buffer.
length = file.Read(buffer, 0, chunkSize);
// 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[chunkSize];
dataToRead = dataToRead - length;
}
// Download completed ok?
if(dataToRead == 0 && Response.IsClientConnected)
{
success = true;
}
}
finally
{
// end the reponse to the user
//HttpContext.Current.ApplicationInstance.CompleteRequest
HttpContext.Current.ApplicationInstance.CompleteRequest();
//Response.End();
}
return success;
}
-Code-
Now this seems to work fine in the cases where:
1. the user is prompted with the Open/Save As dialog, they select Save As,
enter the file name and click ok then the file download completes
successfully.
2. the user is prompted with the Open/Save As dialog and the click Cancel.
This does NOT work in cases where:
1. the user is prompted with the Open/Save As dialog, they select Save As,
then at the stage where they ought to select where to save the file to they
click Cancel. In this circumstance the test "Response.IsClientConnected"
remains "true" even though the user has cancelled the download.
Further to this my investigations have uncovered that the client browser
appears to be buffering the file once the user is presented with the
Open/Save As dialog, this means that in the case where the file is small the
browser may have fully downloaded the file before the user has even selected
where to save the file to using the Save As dialog. So if the user Cancels at
this stage the file may have already been fully written out to the client.
So does anyone have a strategy I can use to:
A. stop the client browser from buffering the file until the user has
selected where to save the file to.
B. instigate a singular file download and then record a successful download.
Cheers,
Sam-Kiwi
ASP.NET
Users purchase documents and then download them.
The intention is that users are only charged for documents they successfuly
download.
My problem revolves around detecting a successful download, the steps I take
to handle the download are as follows:
Chunk the file into 1024byte chunks
Stream each chunk out to the clients browser
After each chunk is sent check if the client is still connected
One all chunks have been streamed out check the client is still connected,
if the client IS still connected then I deam the download successful.
-Code-
/// <summary>
/// Stream the file held in the MemoryStream object out to the client
/// </summary>
/// <param name="file">A MemoryStream containing the file to be streamed
to the client</param>
/// <param name="fileName">A string with the name of the file to be
streamed to the client</param>
/// <returns>A boolean indicating if the stream was successful or
not</returns>
private bool StreamFile(MemoryStream file, string fileName)
{
// reset the position in the file to the start
file.Position = 0;
// Size of the file chunks in bytes
int chunkSize = 1024;
// Buffer to read 1K bytes in chunk:
byte[] buffer = new Byte[chunkSize];
// Length of the buffer content:
int length;
// Total bytes to read:
long dataToRead;
bool success = false;
try
{
// total bytes to read
dataToRead = file.Length;
// Clear the response and add header content
Response.BufferOutput=false;
Response.Buffer=false;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-length", dataToRead.ToString());
Response.AddHeader("Content-Disposition","attachment; filename =" +
fileName);
Response.Flush();
// Write the file out to the client in fileChunkSize pieces
// checking that the client is still connected each time
while(dataToRead > 0 && Response.IsClientConnected)
{
// Read the data in buffer.
length = file.Read(buffer, 0, chunkSize);
// 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[chunkSize];
dataToRead = dataToRead - length;
}
// Download completed ok?
if(dataToRead == 0 && Response.IsClientConnected)
{
success = true;
}
}
finally
{
// end the reponse to the user
//HttpContext.Current.ApplicationInstance.CompleteRequest
HttpContext.Current.ApplicationInstance.CompleteRequest();
//Response.End();
}
return success;
}
-Code-
Now this seems to work fine in the cases where:
1. the user is prompted with the Open/Save As dialog, they select Save As,
enter the file name and click ok then the file download completes
successfully.
2. the user is prompted with the Open/Save As dialog and the click Cancel.
This does NOT work in cases where:
1. the user is prompted with the Open/Save As dialog, they select Save As,
then at the stage where they ought to select where to save the file to they
click Cancel. In this circumstance the test "Response.IsClientConnected"
remains "true" even though the user has cancelled the download.
Further to this my investigations have uncovered that the client browser
appears to be buffering the file once the user is presented with the
Open/Save As dialog, this means that in the case where the file is small the
browser may have fully downloaded the file before the user has even selected
where to save the file to using the Save As dialog. So if the user Cancels at
this stage the file may have already been fully written out to the client.
So does anyone have a strategy I can use to:
A. stop the client browser from buffering the file until the user has
selected where to save the file to.
B. instigate a singular file download and then record a successful download.
Cheers,
Sam-Kiwi