views:

21

answers:

0

Hello. I've got a page that has a button control and several links to download files. The links for the file downloads point to a generic handler, since these files are secured and they will only be returned if the user is logged in.

If I load the page and click the button without doing anything else it works fine, and the event fires. But if I click a link and download a file, the next time I click the button it simply downloads the file again, instead of firing the event.

Any help would be greatly appreciated. Thanks in advance.

Edit: Here is an example link that is generated for downloading a file. These are generated using an ASP.NET LinkButton control inside an ASP.NET repeater control.

<a id="ctl00_Content_LessonFileRepeater_ctl06_FileLinkButton" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$Content$LessonFileRepeater$ctl06$FileLinkButton&quot;, &quot;&quot;, false, &quot;&quot;, &quot;Handlers/FileDownload.ashx?id=02142fe4-12ab-43bf-82f4-5a72b604ab7b&quot;, false, true))">My File.zip</a>

Edit: Here is the code in the download handler.

public void ProcessRequest(HttpContext context)
    {
        if (!AuthenticationHelper.UserIsLoggedIn)
            RedirectToNotAuthorizedPage(context);
        string fileId = context.Request.QueryString["id"];
        if (!Regex.IsMatch(fileId, RegexConstants.Guid))
            RedirectToNotAuthorizedPage(context);
        else
        {
            var lessonFileRepository = new LessonFileRepository();
            LessonFile file = lessonFileRepository.GetById(new Guid(fileId));
            if (file == null)
                RedirectToNotAuthorizedPage(context);
            else
            {
                if (!AuthenticationHelper.CurrentUser.AuthorizedLessons.Any(i => i.LessonFiles.Any(j => j.LessonFileId == file.LessonFileId)))
                    RedirectToNotAuthorizedPage(context);
                else
                {
                    context.Response.Buffer = true;
                    context.Response.Clear();
                    string encodedFileName = context.Server.UrlEncode(file.FileName + '.' + file.FileExtension);
                    encodedFileName = encodedFileName.Replace(@"+",
                                                              context.Request.Browser.Browser == "IE"
                                                                  ? @"%20"
                                                                  : @"_");
                    context.Response.AddHeader("content-length", GetFileLength(context, file.Lesson.LessonNumber, file.FileName+"."+file.FileExtension));
                    context.Response.AddHeader("content-disposition", "attachment; filename=" + encodedFileName + ";");
                    context.Response.ContentType = "application/" + GetContentType(file.FileExtension) + ";";
                    context.Response.WriteFile("~/LessonFiles/Lesson" + file.Lesson.LessonNumber + "/" +
                                               file.FileName + '.' + file.FileExtension);
                    context.Response.End();
                }
            }
        }
    }

    protected string GetFileLength(HttpContext context, int lessonNumber, string fullFileName)
    {
        var fileData = File.OpenRead(context.Server.MapPath("~/LessonFiles/Lesson" + lessonNumber + "/" + fullFileName));
        return fileData.Length.ToString();
    }