File upload in asp.net
web.config re
<connectionStrings>
<add name="inductionConnectionString" connectionString="Data Source=SWASHDBS\SWASHSQLINT;Initial Catalog=RcmSite;Persist Security Info=True;User ID=induction;Password=swash@2015" providerName="System.Data.SqlClient"/>
<!--<add name="inductionConnectionString" connectionString="Data Source=swash.database.windows.net;Initial Catalog=RcmSite;Persist Security Info=True;User ID=swashDB_Admin;password=Swash@2016" providerName="System.Data.SqlClient" />-->
</connectionStrings>
===========================
add in .cs file
string inductionConnectionString = ConfigurationManager.ConnectionStrings["inductionConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = null;
DataSet ds = null;
with in page_load
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/AttatchPath/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
with in button_Save event
{
string fileName = Path.GetFileName(fileUpload1.PostedFile.FileName);
fileUpload1.PostedFile.SaveAs(Server.MapPath("~/AttatchPath/") + result + "-" + fileName);
Session["fileName"] = "~/AttatchPath/" + result + "-" + fileName;
if (Session["fileName"] != null || fileUpload1.HasFile)
{
last re.....
cmd.Connection = con;
con.Open();
int k = cmd.ExecuteNonQuery();
if (k != 0)
{
mailsent();
Response.Write("<script>alert('Inserted Data Succesfully')</script>");
Session["fileName"] = "";
Session["fileName"] = null;
Clear();
}
con.Close();
}
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
string fileName = Path.GetFileName(fileUpload1.PostedFile.FileName);
fileUpload1.PostedFile.SaveAs(Server.MapPath("~/AttatchPath/") + fileName);
ViewState["fileName"] = fileName;
//Response.Redirect(Request.Url.AbsoluteUri);
}
catch (Exception)
{
}
}
}
}
====================================
Design page
<h1>Documents</h1>
<div style="margin-bottom: 2%"></div>
<asp:FileUpload ID="fileUpload1" runat="server" /><br />
<div style="margin-bottom: 1%"></div>
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" Visible="false" />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
<asp:RequiredFieldValidator ID="rfvFileupload" ValidationGroup="FormDefault" runat="server" Display="Dynamic"
ErrorMessage="Please attach a file" ForeColor="Red" ControlToValidate="fileUpload1"></asp:RequiredFieldValidator>
Comments
Post a Comment