آپلود و دانلود فایل های Word از پایگاه داده در ASP.NET
پنجشنبه 31 اردیبهشت 1394در این مقاله میخواهیم نحوه آپلود و دانلود فایل های Word از پایگاه داده در ASP.NET را شرح دهیم
در این مقاله من خواهیم دید که چگونه فایل های Word راآپلود کنیم و درون پایگاه داده ذخیره کنیم و چگونه فایل های ذخیره شده را از پایگاه داده دانلود کنیم و فرمت فایل انتخابی باید doc. یا docx. باشد.
در جدول بالا در فیلد name نام فایل و در فیلد type نوع فایل و در فیلد Data محتوای اصلی فایل به صورت باینری ذخیره میشود.
حال یک پروژه جدید از نوع وب فرم بسازید سپس یک صفحه جدید به نام Default.aspx به پروژه خود اضافه کنید
از جعبه ابزار خود یک کنترل فایل آپلود و دوعدد دکمه ،یک عدد label و یک عدد گریدویو به صفحه خود اضافه کنید
<div> <table> <tr> <td> انتخاب فایل </td> <td> <asp:FileUpload ID="FileUpload1" runat="server" ToolTip="لطفا فایل Word انتخاب کنید" /> </td> <td> <asp:Button ID="Button1" runat="server" Text="آپلود" onclick="Button1_Click" /> </td> <td> <asp:Button ID="Button2" runat="server" Text="نمایش فایل" onclick="Button2_Click" /> </td> </tr> </table> <p><asp:Label ID="Label2" runat="server" Text="label"></asp:Label> </p> <br /> <asp:GridView ID="GridView1" runat="server" CaptionAlign="Top" HorizontalAlign="Justify" DataKeyNames="id" onselectedindexchanged="GridView1_SelectedIndexChanged" ToolTip="Word FIle DownLoad Tool" CellPadding="4" ForeColor="#333333" GridLines="None"> <RowStyle BackColor="#E3EAEB" /> <Columns> <asp:CommandField ShowSelectButton="True" SelectText="Download" ControlStyle-ForeColor="Blue"/> </Columns> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="Gray" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#7C6F57" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> </div>
در رویداد کلیک دکمه آپلود نوع فایل بررسی میشود که در صورت صحیح بودن فایل آپلود شده به صورت باینری در پایگاه داده ذخیره میشود.
protected void Button1_Click(object sender, EventArgs e) { Label2.Visible = true; string filePath = FileUpload1.PostedFile.FileName; string filename1 = Path.GetFileName(filePath); string ext = Path.GetExtension(filename1); string type = String.Empty; if (!FileUpload1.HasFile) { Label2.Text = "لطفا فایل را انتخاب کنید"; } else if (FileUpload1.HasFile) { try { // Added by vithal wadje for Csharp-Corner contribution switch (ext) { case ".doc": type = "application/word"; break; case ".docx": type = "application/word"; break; } if (type != String.Empty) { connection(); Stream fs = FileUpload1.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); Byte[] bytes = br.ReadBytes((Int32)fs.Length); query = "insert into Wordfiles(Name,type,data)" + " values (@Name, @type, @Data)"; com = new SqlCommand(query, con); com.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename1; com.Parameters.Add("@type", SqlDbType.VarChar).Value = type; com.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes; com.ExecuteNonQuery(); Label2.ForeColor = System.Drawing.Color.Green; Label2.Text = "فایل به موفقیت آپلود شد"; } else { Label2.ForeColor = System.Drawing.Color.Red; Label2.Text = "لطفا فقط فایل از نوع Word انتخاب کنید"; } } catch (Exception ex) { Label2.Text = "Error: " + ex.Message.ToString(); } } }
زمانی که روی دکمه نمایش کلیک کنیم لیست فایل ها را در گرید نمایش میدهیم.
protected void Button2_Click(object sender, EventArgs e) { connection(); query = "Select *from Wordfiles"; SqlDataAdapter da = new SqlDataAdapter(query, con); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); }
حال در رویداد SelectedIndexChanged گریدویو id سطر انتخاب شده را پیدا میکنیم و اطلاعات را خوانده و برای دانلود آماده میکنیم:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { connection (); SqlCommand com = new SqlCommand("select Name,type,data from Wordfiles where id=@id", con); com.Parameters.AddWithValue("id", GridView1.SelectedRow.Cells[1].Text); SqlDataReader dr = com.ExecuteReader(); if (dr.Read()) { Response.Clear(); Response.Buffer = true; Response.ContentType = dr["type"].ToString(); Response.AddHeader("content-disposition", "attachment;filename=" + dr["Name"].ToString()); Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.BinaryWrite((byte[])dr["data"]); Response.End(); } }
- ASP.net
- 3k بازدید
- 4 تشکر