473,608 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Memo ryStream 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.Buffer Output=false;
Response.Buffer =false;
Response.Clear( );
Response.Conten tType = "applicatio n/octet-stream";
Response.AddHea der("content-length", dataToRead.ToSt ring());
Response.AddHea der("Content-Disposition","a ttachment; filename =" +
fileName);
Response.Flush( );

// Write the file out to the client in fileChunkSize pieces
// checking that the client is still connected each time
while(dataToRea d > 0 && Response.IsClie ntConnected)
{
// Read the data in buffer.
length = file.Read(buffe r, 0, chunkSize);

// Write the data to the current output stream.
Response.Output Stream.Write(bu ffer, 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.IsClie ntConnected)
{
success = true;
}
}
finally
{
// end the reponse to the user
//HttpContext.Cur rent.Applicatio nInstance.Compl eteRequest
HttpContext.Cur rent.Applicatio nInstance.Compl eteRequest();
//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.IsCli entConnected"
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 3027
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******@discu ssions.microsof t.com> wrote in message
news:F7******** *************** ***********@mic rosoft.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(Memo ryStream 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.Buffer Output=false;
Response.Buffer =false;
Response.Clear( );
Response.Conten tType = "applicatio n/octet-stream";
Response.AddHea der("content-length", dataToRead.ToSt ring());
Response.AddHea der("Content-Disposition","a ttachment; filename =" +
fileName);
Response.Flush( );

// Write the file out to the client in fileChunkSize pieces
// checking that the client is still connected each time
while(dataToRea d > 0 && Response.IsClie ntConnected)
{
// Read the data in buffer.
length = file.Read(buffe r, 0, chunkSize);

// Write the data to the current output stream.
Response.Output Stream.Write(bu ffer, 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.IsClie ntConnected)
{
success = true;
}
}
finally
{
// end the reponse to the user
//HttpContext.Cur rent.Applicatio nInstance.Compl eteRequest
HttpContext.Cur rent.Applicatio nInstance.Compl eteRequest();
//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.IsCli entConnected"
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******@discu ssions.microsof t.com> wrote in message
news:F7******** *************** ***********@mic rosoft.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(Memo ryStream 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.Buffer Output=false;
| Response.Buffer =false;
| Response.Clear( );
| Response.Conten tType = "applicatio n/octet-stream";
| Response.AddHea der("content-length", dataToRead.ToSt ring());
| Response.AddHea der("Content-Disposition","a ttachment; filename =" +
| fileName);
| Response.Flush( );
|
| // Write the file out to the client in fileChunkSize pieces
| // checking that the client is still connected each time
| while(dataToRea d > 0 && Response.IsClie ntConnected)
| {
| // Read the data in buffer.
| length = file.Read(buffe r, 0, chunkSize);
|
| // Write the data to the current output stream.
| Response.Output Stream.Write(bu ffer, 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.IsClie ntConnected)
| {
| success = true;
| }
| }
| finally
| {
| // end the reponse to the user
| //HttpContext.Cur rent.Applicatio nInstance.Compl eteRequest
| HttpContext.Cur rent.Applicatio nInstance.Compl eteRequest();
| //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.IsCli entConnected"
| 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
2888
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 script to invoke something on the server, and if it works, then it works. Is there a better way? I'm looking for the least intrusive way of doing it, from a web application point of view. i.e. I'd like to be able to drop this into an existing...
2
1926
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. The download process takes all of the webserver's resources, typically I see the memory max out. What is the best way to deal with larger downloads without resorting to 3rd part components like SA File Upload? Case "getimage"
2
1245
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 thought that would also allow for large downloads. Apparently not. I've seen postings about large downloads that mention disabling page buffering (or something like that) - I added "Response.BufferOutput = False" to my code, but that didn't fix...
2
1792
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 file is downloaded to the harddisk and then plays. Can I make the file stream in Firefox as well?
1
1522
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
3744
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 relevant name owner? If I make a whisky and call it "Jack Daniels", I most probably will have some serious legal problems. "Mozilla" partially appeared because NCSA stopped them from using "Mosaic" in the UA string. Is it some different...
0
978
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. I've tried turning off buffering and everything - but it refuses to send it out. I am trying to enable partial/resumable downloads for PDF/DOC content that I am streaming up from a SQL image column.
15
4212
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 testing for support of particular eventTypes - the DOM 2 Events Interface DocumentEvent says that if the eventType is not supported, it throws a DOM exception. This makes testing rather tough - if you try something like: if (document &&...
1
3202
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 repeatedly prints out a line of data, flushes output buffers (with appropriate PHP functions), then sleeps for a number of seconds. In other words, this represents a constant input stream to the browser which is requesting the script as a page. On my...
0
8050
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7987
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8472
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8464
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8130
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8324
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3954
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
1318
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.