當前位置:首頁 » 文件管理 » 文件上傳fakepath

文件上傳fakepath

發布時間: 2024-03-30 19:25:53

『壹』 Visual Studio 2008 中用fileupload控制項上傳圖片時控制項中的路徑與alert(path);彈出來的路徑不一樣怎麼回事

倒是不難,但是應用程序要做成工程的這里怎麼寫的下?
以下代碼僅供參考!·
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//初始化文件夾信息
InitFolderInfo();
//初始化上傳限制信息
InitUploadLimit();
//初始化列表框控制項文件列表信息
InitFileList();
}
}
#region 初始化文件夾信息
private void InitFolderInfo()
{
//從config中讀取文件上傳路徑
string strFileUpladPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString();
//如果上傳文件夾不存在,則根據config創建一個
if(!Directory.Exists(Server.MapPath(strFileUpladPath)))
{
Directory.CreateDirectory(Server.MapPath(strFileUpladPath));
}
//將虛擬路徑轉換為物理路徑
string strFilePath = Server.MapPath(strFileUpladPath);
//從config里讀取文件夾容量限制
double iFolderSizeLimit = Convert.ToInt32(ConfigurationManager.AppSettings["FolderSizeLimit"]);
//聲明文件夾已經使用的容量
double iFolderCurrentSize = 0;
//獲取文件夾中的所有文件
FileInfo[] arrFiles = new DirectoryInfo(strFilePath).GetFiles();
//循環文件獲已經使用的容量
foreach (FileInfo fi in arrFiles)
{
iFolderCurrentSize += Convert.ToInt32(fi.Length / 1024);
}
#region 第二種獲得文件夾使用大小的方法
//DirectoryInfo dir = new DirectoryInfo(strFilePath);
//foreach (FileSystemInfo fi in dir.GetFileSystemInfos())
//{
// FileInfo finf = new FileInfo(fi.FullName);
// iFolderCurrentSize += Convert.ToInt32(finf.Length / 1024);
//}
#endregion
//把文件夾容量和以用文件夾容量賦值給標簽
lbl_FolderInfo.Text = string.Format("文件夾容量限制:M,已用容量:KB", iFolderSizeLimit / 1024, iFolderCurrentSize);
}
#endregion
#region 初始化上傳限制信息
private void InitUploadLimit()
{
//從config中讀取上傳文件夾類型限制並根據逗號分割成字元串數組
string[] arrFileTypeLimit = ConfigurationManager.AppSettings["FileTypeLimit"].ToString().Split(',');
//從config中讀取上傳文件大小限制
double iFileSizeLimit = Convert.ToInt32(ConfigurationManager.AppSettings["FileSizeLimit"]);
//遍歷字元串數組把所有項加入項目編號控制項
for (int i = 0; i < arrFileTypeLimit.Length; i++)
{
bl_TileTypeLimit.Items.Add(arrFileTypeLimit[i].ToString());
}
//把文件大小限制賦值給標簽
lab_FileSizeLimit.Text = string.Format("M", iFileSizeLimit / 1024);
}
#endregion
#region 初始化列表框控制項文件列表信息
private void InitFileList()
{
//從config中獲取文件上傳路徑
string strFileUpladPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString();
//將虛擬路徑轉換為物理路徑
string strFilePath = Server.MapPath(strFileUpladPath);
//讀取上傳文件夾下所有文件
FileInfo[] arrFile = new DirectoryInfo(strFilePath).GetFiles();
//把文件名逐一添加到列表框控制項
foreach(FileInfo fi in arrFile)
{
lb_FileList.Items.Add(fi.Name);
}
}
#endregion
#region 判斷文件大小限制
private bool IsAllowableFileSize()
{
//從web.config讀取判斷文件大小的限制
double iFileSizeLimit = Convert.ToInt32(ConfigurationManager.AppSettings["FileSizeLimit"]) * 1024;
//判斷文件是否超出了限制
if (iFileSizeLimit > FileUpload.PostedFile.ContentLength)
{
return true;
}
else
{
return false;
}
}
#endregion
#region 判斷文件類型限制
protected bool IsAllowableFileType()
{
//從web.config讀取判斷文件類型限制
string strFileTypeLimit = ConfigurationManager.AppSettings["FileTypeLimit"].ToString();
//當前文件擴展名是否包含在這個字元串中
if(strFileTypeLimit.IndexOf(Path.GetExtension(FileUpload.FileName).ToLower()) >0)
return true;
else
return false;
}
#endregion
#region 彈出警告消息
protected void ShowMessageBox(string strMessage)
{
Response.Write(string.Format("<script>alert('')</script>",strMessage));
}
#endregion
#region 上傳文件按鈕事件
protected void btn_Upload_Click(object sender, EventArgs e)
{
//判斷用戶是否選擇了文件
if (FileUpload.HasFile)
{
//調用自定義方法判斷文件類型否符合
if (IsAllowableFileType())
{
//判斷文件大小是否符合
if (IsAllowableFileSize())
{
//從web.config中讀取上傳路徑
string strFileUploadPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString();
//從UploadFile控制項中讀取文件名
string strFileName = FileUpload.FileName;
//組合成物理路徑
string strFilePhysicalPath = Server.MapPath(strFileUploadPath + "/") + strFileName;
//判斷文件是否存在
if(!File.Exists(strFilePhysicalPath))
{
//保存文件
FileUpload.SaveAs(strFilePhysicalPath);
//更新列表框
lb_FileList.Items.Add(strFileName);
//更新文件夾信息
InitFolderInfo();
ShowMessageBox("上傳成功!");
}
else
{
ShowMessageBox("文件已經存在!");
}
}
else
{
ShowMessageBox("文件大小不符合要求!");
}
}
else
{
ShowMessageBox("類型不匹配");
}
}
}
#endregion
#region 列表框事件
protected void lb_FileList_SelectedIndexChanged(object sender, EventArgs e)
{
//從config中讀取文件上傳路徑
string strFileUpladPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString();
//從列表框中讀取選擇的文件名
string strFileName = lb_FileList.SelectedValue;
//組合成物理路徑
string strFilePhysicalPath = Server.MapPath(strFileUpladPath + "/") + strFileName;
//根據物理路徑實例化文件信息類
FileInfo fi = new FileInfo(strFilePhysicalPath);
//或得文件大小和創建日期賦值給標簽
lbl_FileDescription.Text = string.Format("文件大小:位元組<br><br>上傳時間:<br>", fi.Length, fi.CreationTime);
//把文件名賦值給重命名文件框
tb_FileNewName.Text = strFileName;
}
#endregion
#region 下載文件按鈕事件
protected void btn_DownLoad_Click(object sender, EventArgs e)
{
//從web.config讀取文件上傳路徑
string strFileUploadPath = ConfigurationManager.AppSettings["FileUplodePath"].ToLower();
//從列表框中讀取選擇的文件
string strFileName = lb_FileList.SelectedValue;
//組合成物理路徑
string FullFileName = Server.MapPath(strFileUploadPath + "/") + strFileName;

FileInfo DownloadFile = new FileInfo(FullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream ";
Response.AppendHeader("Content-Disposition ", "attachment;filename= "
+ HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length ", DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
#endregion
#region 刪除文件
protected void btn_Delete_Click(object sender, EventArgs e)
{
//從config中讀取文件上傳路徑
string strFileUpladPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString();
//從列表框中讀取選擇的文件名
string strFileName = lb_FileList.SelectedValue;
//組合成物理路徑
string strFilePhysicalPath = Server.MapPath(strFileUpladPath + "/") + strFileName;
//刪除文件
System.IO.File.Delete(strFilePhysicalPath);
//更新文件列表框控制項
lb_FileList.Items.Remove(lb_FileList.Items.FindByText(strFileName));
//更新文件夾信息
InitFolderInfo();
//更新文件描述信息
tb_FileNewName.Text = "";
//更新重命名文本框
lbl_FileDescription.Text = "";
//調用自定義消息提示
ShowMessageBox("刪除成功!");
}
#endregion
#region 重命名文件
protected void btn_Rename_Click(object sender, EventArgs e)
{
//從web.config中讀取文件上傳路徑
string strFileUpladPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString();
//從列表框中控制項中讀取選擇的文件名
string strFileName = lb_FileList.SelectedValue;
//重命名文本框或得選擇的文件名
string strFileNewName = tb_FileNewName.Text;
//組合成物理路徑
string strFilePhysicalPath = Server.MapPath(strFileUpladPath + "/") + strFileName;
//組合成新物理路徑
string strFileNewPhysicalPath = Server.MapPath(strFileUpladPath + "/") + strFileNewName;
//文件重命名,即獲取新地址覆蓋舊地址的過程
System.IO.File.Move(strFilePhysicalPath, strFileNewPhysicalPath);
//找到文件列表的匹配項
ListItem li = lb_FileList.Items.FindByText(strFileName);
//修改文字
li.Text = strFileNewName;
//修改值
li.Value = strFileNewName;
//調用自定義方法現實
ShowMessageBox("文件覆蓋成功!");
}
#endregion
#region 下拉列表事件
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = Convert.ToInt32(DropDownList1.SelectedValue);
}
#endregion
}

源代碼已發給你,如果有用就加點分!~

熱點內容
經典的c語言程序 發布:2024-05-03 15:03:24 瀏覽:859
工程加密網 發布:2024-05-03 14:59:55 瀏覽:292
吃冰球解壓 發布:2024-05-03 14:59:10 瀏覽:895
編譯晶元發燙 發布:2024-05-03 14:59:05 瀏覽:549
優化演算法pdf 發布:2024-05-03 14:18:10 瀏覽:291
python演算法書 發布:2024-05-03 14:14:25 瀏覽:736
方舟怎麼加入伺服器閃退 發布:2024-05-03 14:05:27 瀏覽:491
安卓心跳怎麼打出來 發布:2024-05-03 13:59:23 瀏覽:100
存儲標准性 發布:2024-05-03 13:37:07 瀏覽:416
液鹼存儲 發布:2024-05-03 13:21:13 瀏覽:156