Re: detecting successful downloads and browser buffering
From: bruce barker (nospam_brubar_at_safeco.com)
Date: 12/08/04
- Next message: tshad: "Re: Access problems trying to access my log files"
- Previous message: Kumar Reddi: "Re: AutoPostback not seeming to go to Page_load"
- In reply to: Sam-Kiwi: "detecting successful downloads and browser buffering"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 7 Dec 2004 17:54:36 -0800
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" <Sam-Kiwi@discussions.microsoft.com> wrote in message
news:F7619F94-AD63-4300-BE84-E4F32A74F7AD@microsoft.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
- Next message: tshad: "Re: Access problems trying to access my log files"
- Previous message: Kumar Reddi: "Re: AutoPostback not seeming to go to Page_load"
- In reply to: Sam-Kiwi: "detecting successful downloads and browser buffering"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|