592 lines
20 KiB
C#
592 lines
20 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Web;
|
|
using System.Web.UI;
|
|
using System.Web.UI.WebControls;
|
|
using System.Data;
|
|
using System.IO;
|
|
|
|
public partial class admin_filemanager_index : System.Web.UI.Page
|
|
{
|
|
|
|
string db = ConfigurationManager.ConnectionStrings["shopConn"].ConnectionString;
|
|
string p_name = ConfigurationManager.ConnectionStrings["shopConn"].ProviderName;
|
|
string[] systemDirs = ConfigurationManager.AppSettings["systemDirs"].Split(',');
|
|
string scc = ConfigurationManager.AppSettings["shopCarCode"].ToString();
|
|
MyWeb.web_config webconfig = new MyWeb.web_config();
|
|
|
|
protected void Page_Load(object sender, EventArgs e)
|
|
{
|
|
if (!IsPostBack) {
|
|
|
|
if ((Request.Cookies[scc + "_ezFileSort"] != null))
|
|
{
|
|
sortWay.SelectedValue = Request.Cookies[scc + "_ezFileSort"].Value;
|
|
}
|
|
saveSort(false);
|
|
|
|
MyWeb.admin admin = new MyWeb.admin();
|
|
if (admin.isLoign())
|
|
{
|
|
//定義初始路徑
|
|
ViewState["root_path"] = "../../upload/";
|
|
|
|
//參數,如有需要增加,請接續後面
|
|
ViewState["QueryString"] = "&CKEditorFuncNum=" + Convert.ToString(Request["CKEditorFuncNum"]) + "&rtnobj=" + Convert.ToString(Request["rtnobj"]) + "&CKEditor=" + Convert.ToString(Request["CKEditor"]) + "&langCode=" + Convert.ToString(Request["langCode"]);
|
|
|
|
|
|
string http_path = "";
|
|
MyWeb.sql sql = new MyWeb.sql();
|
|
DataTable dt = sql.selectTable(db, p_name, "SELECT pic_url FROM company");
|
|
if (dt.Rows.Count>0){
|
|
http_path = dt.Rows[0]["pic_url"].ToString();
|
|
}
|
|
ViewState["http_path"] = http_path + "/upload/"; //定義上傳圖片實體路徑
|
|
ViewState["total_bytes"] = 0; //定義總檔案大小初始值
|
|
|
|
if (!webconfig.isStrNull(Request["kw"]))
|
|
{
|
|
search.Text = Request["kw"];
|
|
}
|
|
|
|
if (Convert.ToString(Request["mode"]) == "upload")
|
|
{
|
|
MultiView1.ActiveViewIndex = 1;
|
|
L_path.Text = "";
|
|
Image2.Visible = false;
|
|
tr1.Visible = false;
|
|
}
|
|
else
|
|
{
|
|
load_dir_files(); //檔案列表
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
Response.Clear();
|
|
Response.StatusCode = 404;
|
|
Response.End();
|
|
//Panel1.Visible = false;
|
|
//L_login_msg.Text = "<br><br><br><br><br><br>您尚未登錄管理!";
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
#region 檔案列表
|
|
|
|
public void load_dir_files() {
|
|
|
|
//計算總檔案大小
|
|
total_bytes("");
|
|
|
|
DataTable dt = new DataTable("Dt1");
|
|
|
|
//定義DataTable欄位名稱
|
|
dt.Columns.Add(new DataColumn("filename"));
|
|
dt.Columns.Add(new DataColumn("review"));
|
|
dt.Columns.Add(new DataColumn("filesize"));
|
|
dt.Columns.Add(new DataColumn("c_date"));
|
|
dt.Columns.Add(new DataColumn("filename2"));
|
|
dt.Columns.Add(new DataColumn("filekind"));
|
|
dt.Columns.Add(new DataColumn("fileqty"));
|
|
dt.Columns.Add(new DataColumn("isDir"));
|
|
|
|
//取得目錄路徑
|
|
string dirpath = "";
|
|
string dirpath2 = "";
|
|
string dirpath3 = "";
|
|
string dirpath4 = "";
|
|
|
|
if (Convert.ToString(Request["dirname"]) != "" & Request["dirname"]!=null)
|
|
{
|
|
|
|
dirpath = "/" + Convert.ToString(Request["dirname"]);
|
|
dirpath4 = Convert.ToString(Request["dirname"]) + "/";
|
|
string[] v = Convert.ToString(Request["dirname"]).Split('/');
|
|
for (int ii = 0; ii < v.Length; ii++) {
|
|
if (dirpath2 != "")
|
|
{
|
|
dirpath2 += "/" + v[ii];
|
|
if (ii == v.Length - 1)
|
|
{
|
|
dirpath3 += "/" + v[ii];
|
|
}
|
|
else {
|
|
dirpath3 += "/<a href='index.aspx?dirname=" + HttpUtility.UrlEncode(dirpath2) + ViewState["QueryString"].ToString() + "'>" + HttpUtility.HtmlEncode(v[ii]) + "</a>";
|
|
}
|
|
}
|
|
else {
|
|
dirpath2 += v[ii];
|
|
if (ii == v.Length - 1)
|
|
{
|
|
dirpath3 += v[ii];
|
|
}
|
|
else {
|
|
dirpath3 += "<a href='index.aspx?dirname=" + HttpUtility.UrlEncode(dirpath2) + ViewState["QueryString"].ToString() + "'>" + HttpUtility.HtmlEncode(v[ii]) + "</a>";
|
|
}
|
|
}
|
|
}
|
|
L_path.Text = "<a href='index.aspx" + ViewState["QueryString"].ToString().Replace("&CKEditorFuncNum=", "?CKEditorFuncNum=") + "'>根目錄</a>/" + dirpath3;
|
|
}
|
|
else {
|
|
dirpath = "";
|
|
dirpath4 = "";
|
|
L_path.Text = "";
|
|
Image2.Visible = false;
|
|
tr1.Visible = false;
|
|
}
|
|
|
|
string kw ="";
|
|
if (!webconfig.isStrNull(Request["kw"]))
|
|
{
|
|
kw = Request["kw"];
|
|
}
|
|
|
|
//定義資料
|
|
string path = Server.MapPath(ViewState["root_path"].ToString() + dirpath);
|
|
DirectoryInfo dirInfo= new DirectoryInfo(path);
|
|
DirectoryInfo[] subDirs = dirInfo.GetDirectories();
|
|
FileInfo[] subFiles = dirInfo.GetFiles();
|
|
|
|
//將資料夾名稱填入DataTable
|
|
string child_dirinfo = "";
|
|
string new_dir = "";
|
|
for (int i = 0; i < subDirs.Length; i++) {
|
|
|
|
bool chkSearch = true;
|
|
if (kw != "")
|
|
{
|
|
if (subDirs[i].Name.ToLower().IndexOf(kw.ToLower()) == -1)
|
|
{
|
|
chkSearch = false;
|
|
}
|
|
}
|
|
if (chkSearch)
|
|
{
|
|
|
|
//取得子資料夾資訊
|
|
string path2 = Server.MapPath(ViewState["root_path"].ToString() + dirpath + "/" + subDirs[i].Name);
|
|
DirectoryInfo dirInfo2 = new DirectoryInfo(path2);
|
|
DirectoryInfo[] subDirs2 = dirInfo2.GetDirectories();
|
|
FileInfo[] subFiles2 = dirInfo2.GetFiles();
|
|
|
|
child_dirinfo = "內有 <span class=\"text-primary\">" + subDirs2.Length.ToString() + "</span> 資料夾<br><span class=\"text-primary\">" + subFiles2.Length + "</span> 個檔案";
|
|
string fileqty = "";
|
|
string filekind = "";
|
|
|
|
filekind = "A";
|
|
fileqty = "B";
|
|
bool chkAgin = true;
|
|
//upload的目錄下
|
|
if (dirpath == "")
|
|
{
|
|
foreach (string chkDir in systemDirs)
|
|
{
|
|
if (subDirs[i].Name.ToLower() == chkDir.ToLower())
|
|
{
|
|
filekind = "C";
|
|
fileqty = "B";
|
|
chkAgin = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
if (chkAgin & subDirs2.Length == 0 & subFiles2.Length == 0)
|
|
{
|
|
filekind = "A";
|
|
fileqty = "A";
|
|
}
|
|
|
|
|
|
if (Convert.ToString(Request["dirname"]) != "" & Request["dirname"] != null)
|
|
{
|
|
new_dir = Request["dirname"] + "/" + subDirs[i].Name;
|
|
}
|
|
else
|
|
{
|
|
new_dir = subDirs[i].Name;
|
|
}
|
|
|
|
string subfilename = "<span title=\"" + HttpUtility.HtmlEncode(subDirs[i].Name) + "\">" + HttpUtility.HtmlEncode(subDirs[i].Name) + "</span>";
|
|
string review_file = "<a href=\"index.aspx?dirname=" + HttpUtility.UrlEncode(new_dir) + ViewState["QueryString"].ToString() + "\" title=\"" + HttpUtility.HtmlEncode(subDirs[i].Name) + "\"><img src=\"images/dir.png\" border=\"0\" width=\"100\" /></a>";
|
|
|
|
dt.Rows.Add(subfilename, review_file, child_dirinfo, subDirs[i].CreationTime, subDirs[i].Name, filekind, fileqty, 1);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
dirTotal.Text = subDirs.Length.ToString();
|
|
//將檔案名稱填入DataTable
|
|
|
|
for (int j = 0; j < subFiles.Length; j++)
|
|
{
|
|
|
|
bool chkSearch = true;
|
|
if (kw != "")
|
|
{
|
|
if (subFiles[j].Name.ToLower().IndexOf(kw.ToLower()) == -1)
|
|
{
|
|
chkSearch = false;
|
|
}
|
|
}
|
|
if (chkSearch)
|
|
{
|
|
|
|
string[] tmp = subFiles[j].Name.Split('.');
|
|
string fileSubName = tmp[tmp.Length - 1].ToLower();
|
|
string viewPic = "";
|
|
if (fileSubName == "jpg" | fileSubName == "jepg" | fileSubName == "gif" | fileSubName == "png")
|
|
{
|
|
viewPic = "<img src=\"../../App_Script/display.ashx?file=" + dirpath4 + subFiles[j].Name + "&w=120&h=120\" border=\"0\" />";
|
|
}
|
|
else
|
|
{
|
|
viewPic = "<img src=\"images/" + picLog(fileSubName) + ".png\" border=\"0\" />";
|
|
}
|
|
|
|
string subfilename = "<span title=\"" + HttpUtility.HtmlEncode(subFiles[j].Name) + "\">" + HttpUtility.HtmlEncode(subFiles[j].Name) + "</span>";
|
|
string review_file = "<a href=\"#\" title=\"" + HttpUtility.HtmlEncode(subFiles[j].Name) + "\" onclick=\"OpenFile('" + ViewState["http_path"] + dirpath4 + HttpUtility.UrlEncode(subFiles[j].Name) + "');return false;\">" + viewPic + "</a>";
|
|
dt.Rows.Add(subfilename, review_file, (subFiles[j].Length / 1024).ToString("0.00") + " KB", subFiles[j].CreationTime, subFiles[j].Name, "B", "X", 0);
|
|
|
|
}
|
|
}
|
|
|
|
total.Text = subFiles.Length.ToString();
|
|
|
|
dt.DefaultView.Sort = "isDir desc, " + sortWay.SelectedValue.Replace("$", " ");
|
|
|
|
PagedDataSource objPage = new PagedDataSource();
|
|
objPage.DataSource = dt.DefaultView;
|
|
objPage.AllowPaging = true;
|
|
objPage.PageSize = 12;
|
|
int CurPage = 1;
|
|
if (Request["page"]!=null)
|
|
{
|
|
CurPage = Convert.ToInt32(Request["page"]);
|
|
}
|
|
if (CurPage > objPage.PageCount)
|
|
{
|
|
CurPage = objPage.PageCount;
|
|
}
|
|
objPage.CurrentPageIndex = CurPage - 1;
|
|
|
|
for (int i = 1; i <= objPage.PageCount; i++) {
|
|
nowpage.Items.Add(i.ToString());
|
|
}
|
|
|
|
if (nowpage.Items.Count > 0) {
|
|
nowpage.SelectedValue = CurPage.ToString(); //顯示目前第幾頁
|
|
}
|
|
|
|
maxpage.Text = objPage.PageCount.ToString(); //顯示最大頁數
|
|
|
|
Repeater1.DataSource = objPage;
|
|
Repeater1.DataBind();
|
|
}
|
|
|
|
|
|
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
|
|
{
|
|
DataRowView data = (DataRowView)e.Item.DataItem;
|
|
|
|
if (e.Item.ItemIndex % 4 == 3) {
|
|
Panel splitDiv = (Panel)e.Item.FindControl("splitDiv");
|
|
splitDiv.Visible = true;
|
|
}
|
|
|
|
CheckBox CheckBox1 = (CheckBox)e.Item.FindControl("CheckBox1");
|
|
if (data["filekind"].ToString() == "B" | (data["filekind"].ToString() == "A" & data["fileqty"].ToString() == "A"))
|
|
{
|
|
CheckBox1.Visible = true;
|
|
}
|
|
|
|
HiddenField filekind = (HiddenField)e.Item.FindControl("filekind");
|
|
filekind.Value = data["filekind"].ToString();
|
|
|
|
}
|
|
|
|
public string picLog(string subName){
|
|
string rtn = "|ai|asp|aspx|js|jsp|pdf|php|psd|swf|txt|";
|
|
if (rtn.IndexOf("|" + subName + "|", 0) > -1)
|
|
{
|
|
return subName;
|
|
}
|
|
else if (subName == "xls" | subName == "xlsx")
|
|
{
|
|
return "excel";
|
|
}
|
|
else if (subName == "doc" | subName == "docx")
|
|
{
|
|
return "word";
|
|
}
|
|
else if (subName == "zip" | subName == "rar")
|
|
{
|
|
return "zip";
|
|
}
|
|
else if (subName == "htm" | subName == "html")
|
|
{
|
|
return "html";
|
|
}
|
|
else if (subName == "ppt" | subName == "pps" | subName == "pptx" | subName == "ppsx")
|
|
{
|
|
return "powerpoint";
|
|
}
|
|
else if (subName == "bmp" | subName == "tif")
|
|
{
|
|
return "photo";
|
|
}
|
|
else if (subName == "wmv" | subName == "mp4" | subName == "rm" | subName == "mov")
|
|
{
|
|
return "video";
|
|
}
|
|
else
|
|
{
|
|
return "unknown";
|
|
}
|
|
}
|
|
|
|
protected void nowpage_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
GoToPage(Convert.ToInt32(nowpage.SelectedValue));
|
|
}
|
|
|
|
protected void LinkButton1_Click(object sender, EventArgs e)
|
|
{
|
|
int pg = Convert.ToInt32(nowpage.SelectedValue);
|
|
GoToPage((pg - 1 > 0 ? pg - 1 : 1));
|
|
}
|
|
protected void LinkButton2_Click(object sender, EventArgs e)
|
|
{
|
|
int pg = Convert.ToInt32(nowpage.SelectedValue);
|
|
int maxpg = Convert.ToInt32(maxpage.Text);
|
|
GoToPage((pg + 1 <= maxpg ? pg + 1 : maxpg));
|
|
}
|
|
|
|
public void GoToPage(int p) {
|
|
MyWeb.web_config web_config = new MyWeb.web_config();
|
|
string url = web_config.rtnQueryString("page");
|
|
Response.Redirect(url + "&page=" + p.ToString());
|
|
}
|
|
|
|
//計算目前總檔案大小
|
|
public void total_bytes(string dirpath)
|
|
{
|
|
string path = Server.MapPath(ViewState["root_path"].ToString() + dirpath);
|
|
DirectoryInfo dirInfo = new DirectoryInfo(path);
|
|
DirectoryInfo[] subDirs = dirInfo.GetDirectories();
|
|
FileInfo[] subFiles = dirInfo.GetFiles();
|
|
long all_bytes = Convert.ToInt64(ViewState["total_bytes"]);
|
|
for (int j = 0; j < subFiles.Length; j++)
|
|
{
|
|
all_bytes += subFiles[j].Length;
|
|
}
|
|
//Response.Write("路徑=" + dirpath + "<br>");
|
|
//Response.Write("個別資料夾總和=" + (all_bytes / 1024).ToString("0.00") + "<br><br><br>");
|
|
ViewState["total_bytes"] = all_bytes;
|
|
for (int j = 0; j < subDirs.Length; j++)
|
|
{
|
|
total_bytes(dirpath + "/" + subDirs[j].Name);
|
|
}
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region 刪除
|
|
|
|
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
|
|
{
|
|
string[] v = ((ImageButton)sender).CommandArgument.Split('|');
|
|
if (v[1] == "A") {
|
|
string path = Server.MapPath(Convert.ToString(ViewState["root_path"]) + Convert.ToString(Request["dirname"]) + "/" + v[0]);
|
|
DirectoryInfo DirectoryInfo = new DirectoryInfo(path);
|
|
if (DirectoryInfo.Exists)
|
|
{
|
|
try
|
|
{
|
|
DirectoryInfo.Delete();
|
|
Response.Redirect("index.aspx?dirname=" + Convert.ToString(Request["dirname"]) + ViewState["QueryString"].ToString());
|
|
}
|
|
catch (Exception)
|
|
{
|
|
msg.Text = "(刪除失敗,因為要刪除資料夾不是空的!)";
|
|
}
|
|
}
|
|
else {
|
|
msg.Text = "(要刪除的資料夾:<font color=blue>" + v[0] + "</font> 不存在!!)";
|
|
}
|
|
}
|
|
}
|
|
|
|
protected void dell_all_Click(object sender, ImageClickEventArgs e)
|
|
{
|
|
|
|
CheckBox cbox;
|
|
ImageButton img;
|
|
|
|
foreach (RepeaterItem item in Repeater1.Items)
|
|
{
|
|
|
|
cbox = (CheckBox)item.FindControl("CheckBox1");
|
|
img = (ImageButton)item.FindControl("ImageButton1");
|
|
string[] v = img.CommandArgument.Split('|');
|
|
if (cbox.Checked) {
|
|
|
|
string path = Server.MapPath(Convert.ToString(ViewState["root_path"]) + Convert.ToString(Request["dirname"]) + "/" + v[0]);
|
|
HiddenField filekind = (HiddenField)item.FindControl("filekind");
|
|
if (filekind.Value == "A")
|
|
{
|
|
DirectoryInfo DirectoryInfo = new DirectoryInfo(path);
|
|
if (DirectoryInfo.Exists)
|
|
{
|
|
try
|
|
{
|
|
DirectoryInfo.Delete();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
msg.Text = "(刪除失敗,因為要刪除資料夾不是空的!)";
|
|
}
|
|
}
|
|
else {
|
|
msg.Text = "(要刪除的資料夾:<font color=blue>" + v[0] + "</font> 不存在!!)";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
FileInfo FileInfo = new FileInfo(path);
|
|
if (FileInfo.Exists)
|
|
{
|
|
FileInfo.Delete();
|
|
}
|
|
else {
|
|
msg.Text = "(要刪除的檔案:<font color=blue>" + v[0] + "</font> 不存在!!)";
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GoToPage(Convert.ToInt32(nowpage.SelectedValue));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 建資料夾
|
|
|
|
protected void creat_folder_Click(object sender, ImageClickEventArgs e)
|
|
{
|
|
string path = Server.MapPath(Convert.ToString(ViewState["root_path"]) + Convert.ToString(Request["dirname"]) + "/" + folder_name.Text);
|
|
DirectoryInfo DirectoryInfo = new DirectoryInfo(path);
|
|
if (DirectoryInfo.Exists)
|
|
{
|
|
msg.Text = "(資料夾名稱已存在,無法重覆新增!)";
|
|
}
|
|
else
|
|
{
|
|
DirectoryInfo.Create();
|
|
Response.Redirect("index.aspx?dirname=" + Convert.ToString(Request["dirname"]) + ViewState["QueryString"].ToString());
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 上傳檔案
|
|
|
|
protected void Button1_Click(object sender, ImageClickEventArgs e)
|
|
{
|
|
string up_path = Server.MapPath( Convert.ToString(ViewState["root_path"]) + Convert.ToString(Request["dirname"]));
|
|
HttpFileCollection objFileCollection = Request.Files;
|
|
HttpPostedFile file;
|
|
int counter = 0;
|
|
for (int i = 0; i < objFileCollection.Count; i++) {
|
|
file = objFileCollection[i];
|
|
if (file.ContentLength != null) {
|
|
file.SaveAs(up_path + "/" + Path.GetFileName(file.FileName));
|
|
counter++;
|
|
}
|
|
}
|
|
|
|
if (counter == 0)
|
|
{
|
|
msg.Text = "(請選擇要上傳的檔案!)";
|
|
}
|
|
else
|
|
{
|
|
msg.Text = "(上傳【" + counter.ToString() + "】個檔案成功!)";
|
|
Response.Redirect("index.aspx?dirname=" + Convert.ToString(Request["dirname"]) + ViewState["QueryString"].ToString());
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 批次上傳
|
|
|
|
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
|
|
{
|
|
string url = Request.Url.AbsoluteUri;
|
|
if (url.IndexOf(".aspx?", 0) > -1)
|
|
{
|
|
url += "&mode=upload";
|
|
}
|
|
else {
|
|
url += "?mode=upload";
|
|
}
|
|
Response.Redirect(url);
|
|
}
|
|
|
|
protected void Button2_Click(object sender, EventArgs e)
|
|
{
|
|
string url = Request.Url.AbsoluteUri;
|
|
url = url.Replace("&mode=upload", "");
|
|
url = url.Replace("?mode=upload", "");
|
|
string[] v = url.Split('?');
|
|
if (v.Length == 0) {
|
|
url = url.Replace("&CKEditorFuncNum=", "?CKEditorFuncNum=");
|
|
}
|
|
Response.Redirect(url);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 排序方式
|
|
protected void sortWay_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
saveSort(true);
|
|
}
|
|
protected void saveSort(bool refresh)
|
|
{
|
|
Response.Cookies[scc + "_ezFileSort"].Value = sortWay.SelectedValue;
|
|
Response.Cookies[scc + "_ezFileSort"].Expires = DateTime.Now.AddYears(1);
|
|
if (refresh) {
|
|
Response.Redirect(Request.Url.AbsoluteUri);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 搜尋
|
|
|
|
protected void searchBt_Click(object sender, EventArgs e)
|
|
{
|
|
search.Text = search.Text.Trim();
|
|
string kwStr = "";
|
|
if (search.Text != "")
|
|
{
|
|
kwStr = "&kw=" + Server.UrlEncode(search.Text);
|
|
}
|
|
string[] q = { "kw", "page" };
|
|
Response.Redirect(Request.Url.AbsolutePath + webconfig.rtnQueryString(q) + kwStr);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
} |