출처: http://www.progtalk.com/viewarticle.aspx?articleid=39
What is the FileUpload Web User control?
This is a web control written in Visual Studio 2005 using C#. It will allows users to have a quick way to upload, delete, and launch documents. It has two properties which are:
A. Save Path
B. Vitual Path
Screenshots
====================================================
====================================================
The Source Code:
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
public partial class Controls_FileUpload : System.Web.UI.UserControl
{
private string SaveToPath = "";
private string VirtualPath = "";
#region File Upload Control Properties
/// <summary>
/// Save Location of File. Example "C:\TEMP\"
/// </summary>
[Browsable(true), Category("Properties"), Description("Save Location of File")]
public string File_Save_Path
{
get
{
return SaveToPath;
}
set
{
SaveToPath = value;
}
}
/// <summary>
/// Virtual Path of document. Example: "http://servername/filedirpath/"
/// </summary>
[Browsable(true), Category("Properties"), Description("Virtual Path of document")]
public string Virtual_Path
{
get
{
return VirtualPath;
}
set
{
VirtualPath = value;
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
this.lblError.Text = "";
if (!Page.IsPostBack)
{
if (this.VirtualPath.Length == 0)
{
this.lblError.Text = "Virtual Path Property has not been set.";
this.Panel_Control.Enabled = false;
return;
}
else if (this.SaveToPath.Length == 0)
{
this.lblError.Text = "Save To Path Property has not been set.";
this.Panel_Control.Enabled = false;
return;
}
else
{
if (ValidatePath() == true)
{
if (ShowAllFiles() == false)
{
//An error occured.
this.Panel_Control.Enabled = false;
return;
}
}
else
{
this.Panel_Control.Enabled = false;
return;
}
}
}
}
/// <summary>
/// Validates if the Path exists. If it doesn't we try to create it.
/// </summary>
private bool ValidatePath()
{
try
{
if (System.IO.Directory.Exists(SaveToPath) == false)
{
System.IO.Directory.CreateDirectory(SaveToPath);
return true;
}
else
{
return true;
}
}
catch (Exception ex)
{
lblError.Text = "Error: " + ex.Message.ToString();
return false;
}
}
/// <summary>
/// Shows all the files in this directory.
/// </summary>
private bool ShowAllFiles()
{
try
{
//Clear the list box
lstDocs.Items.Clear();
//Get all the directory files
string[] subfiles = Directory.GetFiles(SaveToPath);
foreach (string file_obj in subfiles)
{
string doc_name = System.IO.Path.GetFileName(file_obj);
//text is document name
//value is full path to document
ListItem tempItem = new ListItem(doc_name, file_obj);
//add to list box
lstDocs.Items.Add(tempItem);
}
return true;
}
catch (Exception ex)
{
this.lblError.Text = "Error: " + ex.Message.ToString();
return false;
}
}
/// <summary>
/// Upload File
/// </summary>
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
if (filePSE.PostedFile.FileName == "" || filePSE.PostedFile.ContentLength == 0)
{
return;
}
string path = filePSE.PostedFile.FileName;
string doc_name = System.IO.Path.GetFileName(path);
string err = "";
bool isSafeFile = CheckExtensions(doc_name);
if (isSafeFile == false)
{
return;
}
if (System.IO.File.Exists(SaveToPath + doc_name))
{
this.lblError.Text = "A file with this name already exists.";
return;
}
filePSE.PostedFile.SaveAs(SaveToPath + doc_name);
//Create list item with text as document name
//and value as full path
ListItem item = new ListItem(doc_name, SaveToPath + doc_name);
//add to list box
lstDocs.Items.Add(item);
this.lblError.Text = "File uploaded successfully.";
}
catch (Exception ex)
{
Response.Write("Error Uploading File: " + ex.Message.ToString());
Response.End();
}
}
/// <summary>
/// Delete File
/// </summary>
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (this.lstDocs.Items.Count == 0)
{
lblError.Text = "No items to delete";
return;
}
else if (this.lstDocs.SelectedIndex == -1)
{
lblError.Text = "Please select an item to delete";
return;
}
else
{
//get temp of selected item
string path = lstDocs.SelectedItem.Value;
//delete the file
System.IO.File.Delete(path);
//Remove list item
lstDocs.Items.Remove(lstDocs.SelectedItem);
this.lblError.Text = "File deleted successfully.";
}
}
catch(Exception ex)
{
Response.Write("Error Deleting File: " + ex.Message.ToString());
Response.End();
}
}
/// <summary>
/// Check if it part of allowed extensions
/// </summary>
private bool CheckExtensions(string filename)
{
try
{
/*
if ( (filename.ToLower().LastIndexOf(".doc") == -1) &&
(filename.ToLower().LastIndexOf(".pdf") == -1) &&
(filename.ToLower().LastIndexOf(".ppt") == -1) &&
(filename.ToLower().LastIndexOf(".txt") == -1) &&
(filename.ToLower().LastIndexOf(".xls") == -1) )
*/
if ((filename.ToLower().LastIndexOf(".exe") != -1))
{
this.lblError.Text = "This type of file cannot be uploaded.";
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message.ToString());
Response.End();
//The line below will never execute.
return false;
}
}
/// <summary>
/// Checks if we have any items, and if so generates javascript for double click.
/// </summary>
protected override void OnPreRender(EventArgs e)
{
if (lstDocs.Items.Count > 0)
{
/*
if you want to alert the value for error checking, use the following:
this.lstDocs.Attributes["ondblclick"] = "alert(this.options[this.selectedIndex].text); window.open('" + Virtual_Path + "' + this.options[this.selectedIndex].text );";
instead of
*/
this.lstDocs.Attributes["ondblclick"] = "window.open('" + Virtual_Path + "' + this.options[this.selectedIndex].text );";
}
else
{
this.lstDocs.Attributes.Remove("ondblclick");
}
base.OnPreRender(e);
}
}
Setting up Control
If you do not setup the control, you will get this error message:
====================================================
====================================================
Steps To Set Up The Control
Go to the page where you have the control. Right click the control and select Properties, and you will see the following:
====================================================
====================================================
Modify to your settings, and set to the current default values to demo it:
Run the application.
You will get the following screen, where you can now upload a document. (Try uploading a document)
====================================================
====================================================
Doube click the document and it will Launch in a new window.
Next try to delete the document:
====================================================
You all Set.
Now you can upload, delete, and view documents specified to a directory.
How Can This Control Be Userful to You?
This is a very simple control, which you can expand to keep file information in your database for your projects. You can track files, who its uploaded by, etc.... This way, with little modification, they can see documents that they only uploaded.
The same idea can be applied to different roles, and various different requirements. This control can server as a template which can be resused for different projects.
Good luck.
- See more at: http://www.progtalk.com/viewarticle.aspx?articleid=39#sthash.DfJJ7S5e.dpuf
What is the FileUpload Web User control?
This is a web control written in Visual Studio 2005 using C#. It will allows users to have a quick way to upload, delete, and launch documents. It has two properties which are:
A. Save Path
B. Vitual Path
Screenshots
====================================================
====================================================
The Source Code:
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
public partial class Controls_FileUpload : System.Web.UI.UserControl
{
private string SaveToPath = "";
private string VirtualPath = "";
#region File Upload Control Properties
/// <summary>
/// Save Location of File. Example "C:\TEMP\"
/// </summary>
[Browsable(true), Category("Properties"), Description("Save Location of File")]
public string File_Save_Path
{
get
{
return SaveToPath;
}
set
{
SaveToPath = value;
}
}
/// <summary>
/// Virtual Path of document. Example: "http://servername/filedirpath/"
/// </summary>
[Browsable(true), Category("Properties"), Description("Virtual Path of document")]
public string Virtual_Path
{
get
{
return VirtualPath;
}
set
{
VirtualPath = value;
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
this.lblError.Text = "";
if (!Page.IsPostBack)
{
if (this.VirtualPath.Length == 0)
{
this.lblError.Text = "Virtual Path Property has not been set.";
this.Panel_Control.Enabled = false;
return;
}
else if (this.SaveToPath.Length == 0)
{
this.lblError.Text = "Save To Path Property has not been set.";
this.Panel_Control.Enabled = false;
return;
}
else
{
if (ValidatePath() == true)
{
if (ShowAllFiles() == false)
{
//An error occured.
this.Panel_Control.Enabled = false;
return;
}
}
else
{
this.Panel_Control.Enabled = false;
return;
}
}
}
}
/// <summary>
/// Validates if the Path exists. If it doesn't we try to create it.
/// </summary>
private bool ValidatePath()
{
try
{
if (System.IO.Directory.Exists(SaveToPath) == false)
{
System.IO.Directory.CreateDirectory(SaveToPath);
return true;
}
else
{
return true;
}
}
catch (Exception ex)
{
lblError.Text = "Error: " + ex.Message.ToString();
return false;
}
}
/// <summary>
/// Shows all the files in this directory.
/// </summary>
private bool ShowAllFiles()
{
try
{
//Clear the list box
lstDocs.Items.Clear();
//Get all the directory files
string[] subfiles = Directory.GetFiles(SaveToPath);
foreach (string file_obj in subfiles)
{
string doc_name = System.IO.Path.GetFileName(file_obj);
//text is document name
//value is full path to document
ListItem tempItem = new ListItem(doc_name, file_obj);
//add to list box
lstDocs.Items.Add(tempItem);
}
return true;
}
catch (Exception ex)
{
this.lblError.Text = "Error: " + ex.Message.ToString();
return false;
}
}
/// <summary>
/// Upload File
/// </summary>
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
if (filePSE.PostedFile.FileName == "" || filePSE.PostedFile.ContentLength == 0)
{
return;
}
string path = filePSE.PostedFile.FileName;
string doc_name = System.IO.Path.GetFileName(path);
string err = "";
bool isSafeFile = CheckExtensions(doc_name);
if (isSafeFile == false)
{
return;
}
if (System.IO.File.Exists(SaveToPath + doc_name))
{
this.lblError.Text = "A file with this name already exists.";
return;
}
filePSE.PostedFile.SaveAs(SaveToPath + doc_name);
//Create list item with text as document name
//and value as full path
ListItem item = new ListItem(doc_name, SaveToPath + doc_name);
//add to list box
lstDocs.Items.Add(item);
this.lblError.Text = "File uploaded successfully.";
}
catch (Exception ex)
{
Response.Write("Error Uploading File: " + ex.Message.ToString());
Response.End();
}
}
/// <summary>
/// Delete File
/// </summary>
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (this.lstDocs.Items.Count == 0)
{
lblError.Text = "No items to delete";
return;
}
else if (this.lstDocs.SelectedIndex == -1)
{
lblError.Text = "Please select an item to delete";
return;
}
else
{
//get temp of selected item
string path = lstDocs.SelectedItem.Value;
//delete the file
System.IO.File.Delete(path);
//Remove list item
lstDocs.Items.Remove(lstDocs.SelectedItem);
this.lblError.Text = "File deleted successfully.";
}
}
catch(Exception ex)
{
Response.Write("Error Deleting File: " + ex.Message.ToString());
Response.End();
}
}
/// <summary>
/// Check if it part of allowed extensions
/// </summary>
private bool CheckExtensions(string filename)
{
try
{
/*
if ( (filename.ToLower().LastIndexOf(".doc") == -1) &&
(filename.ToLower().LastIndexOf(".pdf") == -1) &&
(filename.ToLower().LastIndexOf(".ppt") == -1) &&
(filename.ToLower().LastIndexOf(".txt") == -1) &&
(filename.ToLower().LastIndexOf(".xls") == -1) )
*/
if ((filename.ToLower().LastIndexOf(".exe") != -1))
{
this.lblError.Text = "This type of file cannot be uploaded.";
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message.ToString());
Response.End();
//The line below will never execute.
return false;
}
}
/// <summary>
/// Checks if we have any items, and if so generates javascript for double click.
/// </summary>
protected override void OnPreRender(EventArgs e)
{
if (lstDocs.Items.Count > 0)
{
/*
if you want to alert the value for error checking, use the following:
this.lstDocs.Attributes["ondblclick"] = "alert(this.options[this.selectedIndex].text); window.open('" + Virtual_Path + "' + this.options[this.selectedIndex].text );";
instead of
*/
this.lstDocs.Attributes["ondblclick"] = "window.open('" + Virtual_Path + "' + this.options[this.selectedIndex].text );";
}
else
{
this.lstDocs.Attributes.Remove("ondblclick");
}
base.OnPreRender(e);
}
}
Setting up Control
If you do not setup the control, you will get this error message:
====================================================
====================================================
Steps To Set Up The Control
Go to the page where you have the control. Right click the control and select Properties, and you will see the following:
====================================================
====================================================
Modify to your settings, and set to the current default values to demo it:
Run the application.
You will get the following screen, where you can now upload a document. (Try uploading a document)
====================================================
====================================================
Doube click the document and it will Launch in a new window.
Next try to delete the document:
====================================================
You all Set.
Now you can upload, delete, and view documents specified to a directory.
How Can This Control Be Userful to You?
This
is a very simple control, which you can expand to keep file information
in your database for your projects. You can track files, who its
uploaded by, etc.... This way, with little modification, they can see
documents that they only uploaded.
The same idea can be applied
to different roles, and various different requirements. This control
can server as a template which can be resused for different projects.
Good luck.
What is the FileUpload Web User control?
This is a web control written in Visual Studio 2005 using C#. It will allows users to have a quick way to upload, delete, and launch documents. It has two properties which are:
A. Save Path
B. Vitual Path
Screenshots
====================================================
====================================================
The Source Code:
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
public partial class Controls_FileUpload : System.Web.UI.UserControl
{
private string SaveToPath = "";
private string VirtualPath = "";
#region File Upload Control Properties
/// <summary>
/// Save Location of File. Example "C:\TEMP\"
/// </summary>
[Browsable(true), Category("Properties"), Description("Save Location of File")]
public string File_Save_Path
{
get
{
return SaveToPath;
}
set
{
SaveToPath = value;
}
}
/// <summary>
/// Virtual Path of document. Example: "http://servername/filedirpath/"
/// </summary>
[Browsable(true), Category("Properties"), Description("Virtual Path of document")]
public string Virtual_Path
{
get
{
return VirtualPath;
}
set
{
VirtualPath = value;
}
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
this.lblError.Text = "";
if (!Page.IsPostBack)
{
if (this.VirtualPath.Length == 0)
{
this.lblError.Text = "Virtual Path Property has not been set.";
this.Panel_Control.Enabled = false;
return;
}
else if (this.SaveToPath.Length == 0)
{
this.lblError.Text = "Save To Path Property has not been set.";
this.Panel_Control.Enabled = false;
return;
}
else
{
if (ValidatePath() == true)
{
if (ShowAllFiles() == false)
{
//An error occured.
this.Panel_Control.Enabled = false;
return;
}
}
else
{
this.Panel_Control.Enabled = false;
return;
}
}
}
}
/// <summary>
/// Validates if the Path exists. If it doesn't we try to create it.
/// </summary>
private bool ValidatePath()
{
try
{
if (System.IO.Directory.Exists(SaveToPath) == false)
{
System.IO.Directory.CreateDirectory(SaveToPath);
return true;
}
else
{
return true;
}
}
catch (Exception ex)
{
lblError.Text = "Error: " + ex.Message.ToString();
return false;
}
}
/// <summary>
/// Shows all the files in this directory.
/// </summary>
private bool ShowAllFiles()
{
try
{
//Clear the list box
lstDocs.Items.Clear();
//Get all the directory files
string[] subfiles = Directory.GetFiles(SaveToPath);
foreach (string file_obj in subfiles)
{
string doc_name = System.IO.Path.GetFileName(file_obj);
//text is document name
//value is full path to document
ListItem tempItem = new ListItem(doc_name, file_obj);
//add to list box
lstDocs.Items.Add(tempItem);
}
return true;
}
catch (Exception ex)
{
this.lblError.Text = "Error: " + ex.Message.ToString();
return false;
}
}
/// <summary>
/// Upload File
/// </summary>
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
if (filePSE.PostedFile.FileName == "" || filePSE.PostedFile.ContentLength == 0)
{
return;
}
string path = filePSE.PostedFile.FileName;
string doc_name = System.IO.Path.GetFileName(path);
string err = "";
bool isSafeFile = CheckExtensions(doc_name);
if (isSafeFile == false)
{
return;
}
if (System.IO.File.Exists(SaveToPath + doc_name))
{
this.lblError.Text = "A file with this name already exists.";
return;
}
filePSE.PostedFile.SaveAs(SaveToPath + doc_name);
//Create list item with text as document name
//and value as full path
ListItem item = new ListItem(doc_name, SaveToPath + doc_name);
//add to list box
lstDocs.Items.Add(item);
this.lblError.Text = "File uploaded successfully.";
}
catch (Exception ex)
{
Response.Write("Error Uploading File: " + ex.Message.ToString());
Response.End();
}
}
/// <summary>
/// Delete File
/// </summary>
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (this.lstDocs.Items.Count == 0)
{
lblError.Text = "No items to delete";
return;
}
else if (this.lstDocs.SelectedIndex == -1)
{
lblError.Text = "Please select an item to delete";
return;
}
else
{
//get temp of selected item
string path = lstDocs.SelectedItem.Value;
//delete the file
System.IO.File.Delete(path);
//Remove list item
lstDocs.Items.Remove(lstDocs.SelectedItem);
this.lblError.Text = "File deleted successfully.";
}
}
catch(Exception ex)
{
Response.Write("Error Deleting File: " + ex.Message.ToString());
Response.End();
}
}
/// <summary>
/// Check if it part of allowed extensions
/// </summary>
private bool CheckExtensions(string filename)
{
try
{
/*
if ( (filename.ToLower().LastIndexOf(".doc") == -1) &&
(filename.ToLower().LastIndexOf(".pdf") == -1) &&
(filename.ToLower().LastIndexOf(".ppt") == -1) &&
(filename.ToLower().LastIndexOf(".txt") == -1) &&
(filename.ToLower().LastIndexOf(".xls") == -1) )
*/
if ((filename.ToLower().LastIndexOf(".exe") != -1))
{
this.lblError.Text = "This type of file cannot be uploaded.";
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message.ToString());
Response.End();
//The line below will never execute.
return false;
}
}
/// <summary>
/// Checks if we have any items, and if so generates javascript for double click.
/// </summary>
protected override void OnPreRender(EventArgs e)
{
if (lstDocs.Items.Count > 0)
{
/*
if you want to alert the value for error checking, use the following:
this.lstDocs.Attributes["ondblclick"] = "alert(this.options[this.selectedIndex].text); window.open('" + Virtual_Path + "' + this.options[this.selectedIndex].text );";
instead of
*/
this.lstDocs.Attributes["ondblclick"] = "window.open('" + Virtual_Path + "' + this.options[this.selectedIndex].text );";
}
else
{
this.lstDocs.Attributes.Remove("ondblclick");
}
base.OnPreRender(e);
}
}
Setting up Control
If you do not setup the control, you will get this error message:
====================================================
====================================================
Steps To Set Up The Control
Go to the page where you have the control. Right click the control and select Properties, and you will see the following:
====================================================
====================================================
Modify to your settings, and set to the current default values to demo it:
Run the application.
You will get the following screen, where you can now upload a document. (Try uploading a document)
====================================================
====================================================
Doube click the document and it will Launch in a new window.
Next try to delete the document:
====================================================
You all Set.
Now you can upload, delete, and view documents specified to a directory.
How Can This Control Be Userful to You?
This
is a very simple control, which you can expand to keep file information
in your database for your projects. You can track files, who its
uploaded by, etc.... This way, with little modification, they can see
documents that they only uploaded.
The same idea can be applied
to different roles, and various different requirements. This control
can server as a template which can be resused for different projects.
Good luck.