-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadFile.aspx.cs
120 lines (107 loc) · 3.83 KB
/
UploadFile.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.IO;
using System.Web;
namespace CSharpAssignment
{
public partial class UploadFile : System.Web.UI.Page
{
public const int MAXIMUM_FILE_SIZE = 1000000;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GetFile();
if (LstFiles.Items.Count != 0)
{
imgPreview.ImageUrl = "~/images/" + LstFiles.SelectedItem.Text;
lblMapPath.Text = "~/images/" + LstFiles.SelectedItem.Text;
}
}
}
public void GetFile()
{
try
{
String[] files = Directory.GetFiles(Server.MapPath("~/images"));
for (int i = 0; i < files.Length; i++)
{
files[i] = new FileInfo(files[i]).Name;
}
LstFiles.DataSource = files;
LstFiles.DataBind();
LstFiles.SelectedIndex = 0;
}
catch { }
}
public void ResetUploadMsg()
{
lblUploadMsg.Text = "";
imgUploadMsg.ImageUrl = "~/images/icons/none.png";
}
public void ResetDeleteMsg()
{
lblDeleteMsg.Text = "";
imgDeleteMsg.ImageUrl = "~/images/icons/none.png";
}
protected void BtnUpload_Click(object sender, EventArgs e)
{
ResetDeleteMsg();
HttpPostedFile file = fuFile.PostedFile;
if (fuFile.HasFile == false)
{
lblUploadMsg.ForeColor = System.Drawing.Color.Red;
lblUploadMsg.Text = "Select a file to upload";
imgUploadMsg.ImageUrl = "~/images/icons/error.png";
}
else if (fuFile.HasFile == true && file.ContentLength > MAXIMUM_FILE_SIZE)
{
lblUploadMsg.ForeColor = System.Drawing.Color.Red;
lblUploadMsg.Text = "File cannot be greater than 1 MB";
imgUploadMsg.ImageUrl = "~/images/icons/error.png";
}
else
{
string imagePath = Server.MapPath("~/images/" + fuFile.FileName);
fuFile.SaveAs(imagePath);
imgPreview.ImageUrl = "~/images/" + fuFile.FileName;
lblMapPath.Text = "~/images/" + fuFile.FileName;
lblUploadMsg.ForeColor = System.Drawing.Color.Blue;
lblUploadMsg.Text = "Upload succeeded";
imgUploadMsg.ImageUrl = "~/images/icons/confirm.png";
GetFile();
}
}
protected void LstFiles_SelectedIndexChanged(object sender, EventArgs e)
{
imgPreview.ImageUrl = "~/images/" + LstFiles.SelectedItem.Text;
lblMapPath.Text = "~/images/" + LstFiles.SelectedItem.Text;
ResetUploadMsg();
ResetDeleteMsg();
}
protected void BtnDelete_Click(object sender, EventArgs e)
{
ResetUploadMsg();
try
{
File.Delete(MapPath("images") + "\\" + LstFiles.SelectedItem.Text);
GetFile();
if (LstFiles.Items.Count != 0)
{
imgPreview.ImageUrl = "~/images/" + LstFiles.SelectedItem.Text;
lblMapPath.Text = "~/images/" + LstFiles.SelectedItem.Text;
}
else
{
imgPreview.ImageUrl = "";
lblMapPath.Text = "";
}
}
catch
{
lblDeleteMsg.ForeColor = System.Drawing.Color.Red;
lblDeleteMsg.Text = "Choose a file to delete";
imgDeleteMsg.ImageUrl = "~/images/icons/error.png";
}
}
}
}