473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

detecting successful downloads and browser buffering

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.CompleteRe quest
HttpContext.Current.ApplicationInstance.CompleteRe quest();
//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
Nov 19 '05 #1
2 3014
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" <Sa******@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.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.CompleteRe quest
HttpContext.Current.ApplicationInstance.CompleteRe quest();
//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

Nov 19 '05 #2
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)

"Sam-Kiwi" <Sa******@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.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.CompleteRe quest
| HttpContext.Current.ApplicationInstance.CompleteRe quest();
| //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
Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Ryan Stewart | last post by:
I'm working on a project to collect web application usage statistics. What are the recommended ways of detecting whether a browser is JavaScript enabled and/or capable? Obviously I can write a...
2
by: Tim | last post by:
I'm using the code below to enable downloads from my site. This works well for small files, but larger ones (10Mb+) effectively cause the site to lock up, or run very slowly for all other users....
2
by: Jerry Camel | last post by:
I know I've seen postings on this, but I can't find them anymore... I was able to fix the issue of large uploads by adding <httpRuntime maxRequestLength="1048576" /> to the web.config file. I...
2
by: Andreas Müller | last post by:
Hi! I have a WMV file ready for download. When opneing through IE, the file streams correctly - just a few seconds of buffering and here we go. When opening the same file in Firefox, the whole...
1
by: Hello | last post by:
Scenario: A page creates a pop up and that pop up submits a form. Is there a way to detect whether the submitting was successful or not, so true/false could be passed to the parent window?
79
by: VK | last post by:
I wandering about the common proctice of some UA's producers to spoof the UA string to pretend to be another browser (most often IE). Shouldn't it be considered as a trademark violation of the...
0
by: Fil Mackay | last post by:
Has anyone got any experience with getting partial downloads working with ASPX pages? I am unable to .AddHeader("Accept-Range", "bytes") - ASP.NET eats the header, and refuses to transmit it....
15
by: RobG | last post by:
When using createEvent, an eventType parameter must be provided as an argument. This can be one of those specified in DOM 2 or 3 Events, or it might be a proprietary eventType. My problem is...
1
by: coolsti | last post by:
I am having a curious problem with Vista working on a Lenovo thinkpad. To test and demonstrate this problem, I have a PHP script that does not terminate. It enters an infinite loop where it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.