C# 上传文件到指定目录,单击下载文件,删除指定目录文件,打开文件列表
来源:互联网 发布:福彩双色球软件靠谱吗 编辑:程序博客网 时间:2024/05/16 06:54
命名空间: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);
-----------
protected void btnSure_Click(object sender, EventArgs e)
{
//string phName = this.txtName.Text;
//string phType = this.ddlType.SelectedValue;
if (this.myFile.PostedFile != null) //myFile是上传控件的名称
{
string photoName1 = myFile.PostedFile.FileName; //获取初始文件名
int i = photoName1.LastIndexOf("."); //取得文件名中最后一个"."的索引
string newext = photoName1.Substring(i); //获取文件扩展名
if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png")
{
Response.Write("文件格式不正确!");
Response.End();
}
DateTime now = DateTime.Now; //获取系统时间
string classid = DateTime.Now.Year.ToString();
//根据年份判断在该路径下是否存在以当年年份文件夹 否则将建立以该年份的文件夹
//HttpContext.Current.Server.MapPath(相对路径):把相对路径转为服务器上的绝对路径。 File.Exists(绝对路径):检查是否存在绝对路径指向的文件或目录。
if (!Directory.Exists(HttpContext.Current.Server.MapPath("photos/") + "\\" + classid))
{
System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("photos/") + "\\" + classid);
//System.IO.Directory.CreateDirectory(文件夹绝对路径):建立绝对路径文件夹。
}
string photoName2 = now.Millisecond.ToString() + "_" + myFile.PostedFile.ContentLength.ToString() + newext; //重新为文件命名,时间毫秒部分+文件大小+扩展名
// 保存文件到路径,用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
myFile.PostedFile.SaveAs(Server.MapPath("photos\\"+classid+"\\" + photoName2));
}
}
-----------
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();
}
}
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("no");
}
}
4.得到文件夹下的文件列表
-----------
// 得到当前可用的文件列表
private void fn_getCurrFileList(bool IsAlert)
{
try
{
//查找xmlzip文件夹下 属于其本身UnitCoding的相关zip文件
string strXmlZipDirectory = Server.MapPath("../xmlzip/");
if (Directory.Exists(strXmlZipDirectory))
{
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();
}
}
本文转自:http://www.cnblogs.com/freeliver54/archive/2007/01/18/623697.html
- C# 上传文件到指定目录,单击下载文件,删除指定目录文件,打开文件列表
- C#上传文件到网站指定目录
- 上传文件到指定目录
- C# 打开指定目录并定位到文件
- C# 打开指定目录并定位到文件
- C# 打开指定目录并定位到文件
- 将上传文件保存到指定目录
- 打开指定目录 and 打开指定文件
- android 将文件下载到指定目录
- C# Http地址下载文件到指定目录。
- Java 删除目录 指定文件
- C# 删除指定目录下的 所有指定文件、文件夹
- 拷贝文件到指定目录
- 创建文件到指定目录
- 拷贝文件到指定目录
- 从ftp下载指定文件到指定的目录
- ftp下载指定目录指定文件
- JAVA基础回顾2 遍历指定文件目录 遍历删除目录 指定文件清单列表
- PHP连接mysql数据库,调用Google Map显示不同地方地图信息,使用Javascript弹出悬浮信息窗口展示。
- KMP算法 字符匹配
- Ogre中画折线
- Expect:100-Continue & HTTP 417 Expectation
- 每次都查fopen模式,烦人
- C# 上传文件到指定目录,单击下载文件,删除指定目录文件,打开文件列表
- linux下递归删除目录下所有文件
- php函数-- array_pop() 和array_splice()
- XML介绍之XML使用CSS与XSL修饰
- magento custom product api (2) --- getskubyurl
- 游戏数据分析-基本指标
- AVR MCU 选型指南
- java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/Str
- 基于Ubuntu 11.10/12.04编译OpenCV 2.3.x和2.4.x(适用于Fedora和红帽子rhel 6.2)