detecting successful downloads and browser buffering

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
 
B

Ben Lucas

I don't think there is any way to definitively identify the user's actions
from the server since they are all taking place on the client side. In the
case where the user clicks cancel, all that the server knows is that the
entire file was sent. Likewise, with regard to buffering, once the file has
left the server, it's up to the client-side to determine what to do with it.
If it decides to save it before giving the user the option to cancel, that's
all a part of the client side.

About the only way I could see to get around this would be to create a
program that you expect the user to run on the client. This program would
be responsible for making downloads and reporting back to the server if the
download was "completed" If you wanted it to be browser-based, it could be
something like an ActiveX control.

Out of curiousity, why not just charge the user at the time they make the
request. If the concern is that the download may fail, you could provide a
means to access the file (perhaps for a period of time) so that the user
could re-try the download.

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com


Sam-Kiwi said:
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
 
B

bruce barker

also if keepalive is turned off, IIS will close the connection as soon as
its transmitted the data. also if the user is going thru a proxy, your
connection is to the proxy, not the client. the proxy may buffer the whole
download before sending it to the client (even if the client got bored of
waiting, and requested a different page).

to bullet proof the download, you would need to supply an active/x control
that wrote the file and updated the server on successful write to disk. it
would also want to crc the file to see that it was not corrupt.

-- bruce (sqlwork.com)



| 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top