C# :- File Upload On Server And Delete Image in Image Folder
File Upload On Server And Delete Image in Image Folder
Point :- This programe run only on the server not local
Design :-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Height="33px" onclick="Button1_Click"
Text="Upload" Width="75px" />
<br />
<asp:Image ID="Image1" runat="server" Height="128px" Width="125px" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:Button ID="Button2" runat="server" Height="36px" onclick="Button2_Click"
Text="Delete" Width="72px" />
 
</div>
</form>
</body>
</html>
Code :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
FileUpload1.SaveAs(Server.MapPath(@"~/UploadAndDelete/pic/") + "//pic//" + FileUpload1.FileName);
//FileUpload1.SaveAs(Server.MapPath("~") + "/pic/" + FileUpload1.FileName);
Image1.ImageUrl = "~/UploadAndDelete/pic/" + FileUpload1.FileName;
Label1.Text = FileUpload1.FileName;
}
protected void Button2_Click(object sender, EventArgs e)
{
string filename = Label1.Text;
string path1 = Server.MapPath(@"~/UploadAndDelete/pic/");
FileInfo f1 = new FileInfo(path1 + filename);
if (f1.Exists)
{
f1.Delete();
Response.Write("file deleted succesfully");
}
else
{
Response.Write("file not found");
}
}
}
Comments
Post a Comment