dosyaIndirme.aspx
<asp:Panel ID="pnl_urunduzenle" BackColor="#D3E6E6" ForeColor="White" runat="server" > <asp:Repeater ID="rpturunListe" runat="server" OnItemCommand="rpturunListe_ItemCommand"> <HeaderTemplate> <table style="width: 660px"> <tr> <td style="width: 294px; align-content:center;padding-left:10px; color:#4E6262;border:1px double #2a595c">Dosya ADI</td> <td style="width:160px;padding-left:10px; color:#4E6262;border:1px double #2a595c">DÜZENLE</td> <td style="padding-left:10px; color:#4E6262;border:1px double #2a595c">SİL</td> </tr> </table> </HeaderTemplate> <ItemTemplate> <table style="width: 654px;"> <tr style="border: 1px dotted #2a595c;"> <td style="width: 316px;color:#2a595c; margin-right:50px; background-color:#AFC2C2; height: 36px;"> <asp:Literal ID="Literal1" runat="server" Text='<%# Eval("dosya") %>'></asp:Literal> </td> <td style="width: 176px; color:#2a595c; margin-right:50px; background-color:#AFC2C2;height: 36px;"> <asp:LinkButton ID="btnIndir" CommandName="Indir" CommandArgument='<%#Eval("urunID") %>' runat="server">İndir</asp:LinkButton> </td> <td style="width: 176px; color:#2a595c;margin-right:50px; background-color:#AFC2C2;height: 36px;"> <asp:LinkButton ID="btnSil" CommandName="Sil" CommandArgument='<%#Eval("urunID")%>' runat="server">Sil</asp:LinkButton> </td> </tr> </table> </ItemTemplate> </asp:Repeater> </asp:Panel>
dosyaIndirme.aspx.cs
public partial class dosyaIndirme : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { rpturunListe.DataSource = Erdi.EF.uruns.Where(x=>x.dosya!=" ").Select(y => new { y.dosya, y.urunID }).ToList(); rpturunListe.DataBind(); } protected void rpturunListe_ItemCommand(object source, RepeaterCommandEventArgs e) { int urunID = Convert.ToInt32(e.CommandArgument); if (e.CommandName == "Sil") { urun urun = Erdi.EF.uruns.Where(x => x.urunID == urunID ).SingleOrDefault(); urun.dosya = " "; Erdi.EF.SaveChanges(); Response.Redirect("dosyaIndirme.aspx?sil=ok"); } if (e.CommandName == "Indir") { urun urun = Erdi.EF.uruns.Where(x => x.urunID == urunID ).SingleOrDefault(); string filename = urun.dosya; string path = MapPath("~/dosya/" + filename); byte[] bts = System.IO.File.ReadAllBytes(path); try { Response.AddHeader("Content-Disposition", "attachment; filename=" + urun.dosya);//hangi dosya indirilecek Response.AddHeader("Content-Lenght", urun.dosya.Length.ToString());//dosya uzunluğu bildirilir Response.AddHeader("Content-Type", "Application/octet-stream"); Response.BinaryWrite(bts); Response.Flush(); Response.Clear(); } catch (Exception) { Response.Redirect("dosyaIndirme.aspx"); } Response.Flush(); Response.Clear(); } } }
Burada models yapısı kullanıldığı için nesnelere Erdi.EF classı üzerinden ulaşıyoruz. Dosya klasöründe yer alan pdf'lere ulaşıp indirebilmekteyiz.
Sondaki responce flush 'ı sunucu, http üstbilgileri gönderildikten sonra üstbilgi ekleyemiyor. hatasının çözümü için ekledim.
0 yorum:
Yorum Gönder