How to determine if a file is loaded

I have the following code that allows the user to upload a file. I need to know (if possible) if they downloaded the file successfully. Is there any callback that I can bind to see if they were successful in loading it? Thank.

string filename = Path.GetFileName(url);
context.Response.Buffer = true;
context.Response.Charset = "";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = "application/x-rar-compressed";
context.Response.AddHeader("content-disposition", "attachment;filename=" + filename);
context.Response.TransmitFile(context.Server.MapPath(url));
context.Response.Flush();

      

+2


source to share


3 answers


Why not add another line to let you know it's finished? After context.Response.Flush (), this should be done.



+4


source


You can do something like this:




try
{
    Response.Buffer = false;
    Response.AppendHeader("Content-Disposition", "attachment;filename=" + file.Name);
    Response.AppendHeader("Content-Type", "application/octet-stream");
    Response.AppendHeader("Content-Length", file.Length.ToString());

    int offset = 0;
    byte[] buffer = new byte[64 * 1024]; // 64k chunks
    while (Response.IsClientConnected && offset < file.Length)
    {
        int readCount = file.GetBytes(buffer, offset,
            (int)Math.Min(file.Length - offset, buffer.Length));
        Response.OutputStream.Write(buffer, 0, readCount);
        offset += readCount;
    }

    if(!Response.IsClientConnected)
    {
        // Cancelled by user; do something
    }
}
catch (Exception e)
{
    throw new HttpException(500, e.Message, e);
}
finally
{
    file.Close();
}
      

+2


source


I think it's impossible.

The answer is just a memory object that communicates with IIS. You cannot know if the browser has downloaded the entire file, as the user can undo it until the last byte, but after IIS finishes sending the entire stream.

You can try to implement IHttpHandler, write chunks of the file continuously to the context. Answer in Process () and Flush () method and check how it is

context.Response.Flush();
if (!context.Response.IsClientConnected)
// handle disconnect

      

This is the closest I can think of to solve your problem.

0


source







All Articles