using system.io;
1.文件上傳
----------
如下要點:
html部分:
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<input id="fileupload" type="file" runat="server"/><br />
后臺cs部分 按鈕事件
//string strfilefullname = system.io.path.getfilename(this.fileupload.postedfile.filename);
//this.fileupload.postedfile.saveas(server.mappath("./xmlzip/") + strfilefullname);
2.文件下載
----------
listbox的selectedindexchanged事件 設定相關下載連接
protected void lst_downloadfilelist_selectedindexchanged(object sender, eventargs e)
{
try
{
string strjs = "window.open('xmlzip/";
strjs += this.lst_downloadfilelist.selecteditem.text.trim();
strjs += "'); return false; ";
this.imgbtn_downloadfile.attributes.add("onclick", strjs);
}
catch (exception ex)
{
ex.tostring();
}
}
或者也可以通過 改變label的text值 來實現點擊后實現文件下載的超級連接
this.label1.text = "<a href=/"xmlzip/a.rar/">a.rar</a>"
3.文件刪除
---------
string strfilepath = server.mappath("../countryflowmgr/xmlzip/"+this.lst_downloadfilelist.selecteditem.text.trim());
if (file.exists(strfilepath))
{
file.delete(strfilepath);
if (file.exists(strfilepath))
{
response.write("ok");
}
else
{
response.write("ok");
}
}
4.得到文件夾下的文件列表
-----------
#region 得到當前可用的文件列表
/// <summary>
/// 得到當前可用的文件列表
/// </summary>
/// <param name="isalert">是否需要彈出提示信息</param>
private void fn_getcurrfilelist(bool isalert)
{
try
{
//查找xmlzip文件夾下 屬于其本身unitcoding的相關zip文件
string strxmlzipdirectory = server.mappath("../xmlzip/");
if (directory.exists(strxmlzipdirectory))
{
//directoryinfo di = new directoryinfo(environment.currentdirectory);
directoryinfo di = new directoryinfo(strxmlzipdirectory);
fileinfo[] fi = di.getfiles("*.zip");//只查.zip文件
if (fi.length > 0)
{
lst_downloadfilelist.items.clear();
foreach (fileinfo tmpfi in fi)
{
listitem tmpitem = new listitem();
tmpitem.text = tmpfi.name;
lst_downloadfilelist.items.add(tmpitem);
}
lst_downloadfilelist.selectedindex = 0;
}
else
{
if (isalert)
{
response.write("查無可以下載的文件!");
}
}
}
}
catch (exception ex)
{
ex.tostring();
}
}
#endregion
新聞熱點
疑難解答