Asp.net Web Forms Upload File Save Sql Database
Hi Friends,
In this post we will see how we can upload and download files in asp.net. We will upload files and its proper noun and shop it in sql server database and retrieve back the data to display information technology on asp.net gridview. So allow's commencement.
ASP.NET Upload and Download Files:
DOWNLOAD SOURCE CODE
Create a webform with name WebForm2.aspx and edit it as beneath:
WebForm.aspx:
<%@ Page Language="C#" AutoEventWireup="truthful" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>ASP.Cyberspace Uploading and Downloading Files</title> <link href="bootstrap.min.css" rel="stylesheet" /> <mode> .mytable { width: 50%; margin: 10px; } body { padding: 5px; } </way> </head> <torso> <form id="form1" runat="server"> <div> <h2>ASP.Cyberspace - Uploading and Downloading files</h2> <table class="table table-condensed mytable"> <tr> <td>File Name: </td> <td> <asp:TextBox runat="server" ID="txtFileName" Width="150" /> </td> </tr> <tr> <td>Select File: </td> <td> <asp:FileUpload runat="server" ID="fileupload1" /> </td> </tr> <tr> <td colspan="2"> <asp:Button runat="server" ID="btnUpload" OnClick="btnUpload_Click" Text="Submit" CssClass="btn btn-success" /> </td> </tr> </table> <asp:Label runat="server" ID="lblInfo" /> <asp:GridView runat="server" ID="gridviewFiles" UseAccessibleHeader="true" CssClass="table tabular array-hover table-condensed mytable" AutoGenerateColumns="faux"> <Columns> <asp:BoundField HeaderText="Id" DataField="Id" /> <asp:BoundField HeaderText="File name" DataField="FileN" /> <asp:TemplateField HeaderText="File"> <ItemTemplate> <asp:LinkButton runat="server" Text='<%# Eval("FilePath") %>' OnClick="FileDownload_Clicked"/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
This form contains an asp.cyberspace file upload control to upload files and a textbox to enter file name (similar a clarification of uploaded files) and a gridview with download link to the stored file. The download link is shown using asp:linkbutton which will display the file name stored in the database. So when the user is uploading file we will first file in a folder and stored its name in sql database using which nosotros can think the file back.
asp.net file upload and download with sql database preview
And edit the code as below:
WebForm2.aspx.cs:
using System; using System.Data; using System.Data.SqlClient; using Arrangement.IO; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class WebForm2 : Arrangement.Web.UI.Page { SqlConnection con; SqlDataAdapter adapter; SqlCommand cmd; static String connectionString = @"Data Source=192.168.100.45;Initial Itemize=ParallelCodes;User ID=sa;Password=789;Integrated Security=True;"; protected void Page_Load(object sender, EventArgs e) { FillGrid(); } protected void btnUpload_Click(object sender, EventArgs e) { try { String filePath = Server.MapPath("~/Files/" + fileupload1.FileName); fileupload1.SaveAs(filePath); lblInfo.Text = "Your file was successfully uploaded to : Files/" + Path.GetFileName(fileupload1.FileName).ToString(); con = new SqlConnection(connectionString); cmd = new SqlCommand(); con.Open(); cmd.Connection = con; cmd.CommandType = System.Information.CommandType.StoredProcedure; cmd.CommandText = "sp_AddFiles"; cmd.Parameters.AddWithValue("@FileN", txtFileName.Text.ToString()); cmd.Parameters.AddWithValue("@FilePath", fileupload1.FileName); cmd.ExecuteNonQuery(); cmd.Dispose(); con.Dispose(); con.Shut(); lblInfo.Text = "Saved successfully."; txtFileName.Text = ""; FillGrid(); } catch (Exception ex) { lblInfo.Text = ex.Message.ToString(); } } public void FillGrid() { try { con = new SqlConnection(connectionString); cmd = new SqlCommand(); con.Open(); cmd = new SqlCommand("Select * from tblFiles", con); adapter = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); adapter.Fill up(dt); gridviewFiles.DataSource = dt; gridviewFiles.DataBind(); cmd.Dispose(); con.Dispose(); con.Close(); } grab (Exception ex) { } } protected void FileDownload_Clicked(object sender, EventArgs e) { try { var element = (LinkButton)sender; String filename = element.Text.ToString(); String filepath = Server.MapPath("Files/" + filename); Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.AddHeader("Content-Disposition", "zipper; filename=" + filename); Response.Affluent(); Response.TransmitFile(filepath); Response.Cease(); } grab(Exception ex) { } } } }
This is my database script which I used to stored all the information for uploading and downloading file.
dbscript.sql:
create tabular array tblFiles ( Id int primary key identity(1,1) not null, FileN nvarchar(50), FilePath nvarchar(max) ) change procedure sp_AddFiles ( @FileN nvarchar(50),@FilePath nvarchar(max) ) as begin Insert into tblFiles (FileN,FilePath) values (@FileN,@FilePath) finish --truncate table tblFiles ---Select * from tblFiles
DOWNLOAD SOURCE Code
Also run across:
Athwart JS Demark Filigree Html Table from SQL in ASP.NET MVC C#.
Creating AngularJS Login form in MVC ASP.Cyberspace Forms C#.
How to Upload File ASP.NET MVC with Database.
How to upload files in ASP.Internet?
How to download files in ASP.Cyberspace?
ASP.Net Gridview CSS design using Bootstrap.
Source: https://parallelcodes.com/asp-net-how-to-upload-and-download-files-with-sql-database/
0 Response to "Asp.net Web Forms Upload File Save Sql Database"
Post a Comment