ASP.NET Response.End Problem

I have a page loading files from a file. And when the user clicks the download link and finishes downloading the file, I insert a new entry for my File Upload Failure event. (FileID, Date, blalbla ..)

But there is a problem with my script ... after starting the download and finishing the download, adding a new entry, but after that it fires the event again and writes a new entry. So 1 download and 2 new download entries. here is my script;

 if (Page.Request.QueryString["FileID"] != null)
        {
            OleDbCommand command = new OleDbCommand("select * from Dosyalar where DosyaID=" + Page.Request.QueryString["FileID"].ToString(), veri.baglan());

            string dosyaAdi = "";
            int DosyaID = 0;
            OleDbDataReader dr = command.ExecuteReader();
            while (dr.Read())
            {

                dosyaAdi = Server.MapPath("formlar") + "\\" + dr["URL"].ToString();
                DosyaID = Convert.ToInt32(dr["FileID"]);

            }
            dr.Close();

            FileInfo dosya = new FileInfo(dosyaAdi);
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + dosya.Name);
            Response.AddHeader("Content-Length", dosya.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(dosyaAdi);
           // INSERT A NEW RECORD 
            OleDbCommand ekle = new OleDbCommand("Insert into Indirilenler (FileID,Tarih) values (@p1,@p2)", veri.baglan());
            ekle.Parameters.AddWithValue("p1", FileID);
            ekle.Parameters.AddWithValue("p2", DateTime.Now.ToShortDateString());
            ekle.ExecuteNonQuery();
            Response.Flush();
            Response.End();

      

0


a source to share


1 answer


The problem is that the browser actually makes two requests to get the file. This is typical behavior.

Alternatively, you can enable OutputCaching on this page. This should mean that the second browser request will not hit your server and therefore only write one entry, but you also need to ensure that there is no debug flag in your web.config file.



Another option is to define the type of request. I think the first request the browser makes is often a HEAD request, not a GET or POST. You can detect and handle them appropriately, but I think the first option might be more reliable.

Also, you should really code this as an ASPX handler, not a page. This means that you can avoid response.Clear()

and thus not confuse the browser as to what type of URI content exists.

+2


a source







All Articles