using system.io;
//检查上传文件不为空
if(file1.postedfile!=null)
{
string nam = file1.postedfile.filename ;
//取得文件名(抱括路径)里最后一个"."的索引
int i= nam.lastindexof(".");
//取得文件扩展名
string newext =nam.substring(i);
//这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
datetime now = datetime.now;
string newname=now.dayofyear.tostring()+file1.postedfile.contentlength.tostring();
//保存文件到你所要的目录,这里是iis根目录下的upload目录.你可以改变.
//注意: 我这里用server.mappath()取当前文件的绝对目录.在asp.net里""必须用""代替
file1.postedfile.saveas(server.mappath("upload"+newname+newext)); this.hyperlink1.navigateurl ="upload"+newname+newext; //得到这个文件的相关属性:文件名,文件类型,文件大小
//fname.text=file1.postedfile.filename;
//fenc.text=file1.postedfile.contenttype ;
//fsize.text=file1.postedfile.contentlength.tostring();
}
上传可以用.net里的html控件里的file field的上传控件呀,你拖到窗体上后,你可以右击做为服务器端控件使用,就这样写上你要上传的几句代码就行了,下载直接连接到你要下载的文件就可以下载了把文件上传放到服务器上,直接加超衔接就是下载了.一下是上传文件用到的类:
说明:直接在cs文件里复制粘贴就可以用的.using system;
using system.io;
using system.web.ui.htmlcontrols;namespace youjian
{
/// <summary>
/// upfile 的摘要说明。
/// </summary>
public class upfile
{
public upfile()
{
}#region 是否允许该扩展名上传isallowedextension
///<summary>
///是否允许该扩展名上传
///</summary>
///<paramname = "hifile">htmlinputfile控件</param>
///<returns>允许则返回true,否则返回false</returns>
public bool isallowedextension(htmlinputfile hifile)
{
string stroldfilepath = "";
string strextension = ""; //允许上传的扩展名,可以改成从配置文件中读出
string[]arrextension = {".gif",".jpg",".jpeg",".bmp",".png"}; if(hifile.postedfile.filename != string.empty)
{
stroldfilepath = hifile.postedfile.filename;
//取得上传文件的扩展名
strextension = stroldfilepath.substring(stroldfilepath.lastindexof("."));
//判断该扩展名是否合法
for(int i = 0;i<arrextension.length;i++)
{
if(strextension.equals(arrextension[i]))
{
return true;
}
}
}
return false;
}
#endregion #region 判断上传文件大小是否超过最大值isallowedlength
/// <summary>
/// 判断上传文件大小是否超过最大值
/// </summary>
/// <param name="hifile">htmlinputfile控件</param>
/// <returns>超过最大值返回false,否则返回true.</returns>
public bool isallowedlength(htmlinputfile hifile)
{
//允许上传文件大小的最大值,可以保存在xml文件中,单位为kb
int i = 20;
//如果上传文件的大小超过最大值,返回flase,否则返回true.
if(hifile.postedfile.contentlength > i * 1024)
{
return false;
}
return true;
}
#endregion
#region 获取一个不重复的文件名getuniquestring
/// <summary>
/// 获取一个不重复的文件名
/// </summary>
/// <returns></returns>
public string getuniquestring()
{
//得到的文件名形如:20050922101010
return datetime.now.tostring("yyyymmddhhmmss");
}
#endregion #region 删除指定文件deletefile
/// <summary>
/// 删除指定文件
/// </summary>
/// <param name="strabsolutepath">文件绝对路径</param>
/// <param name="strfilename">文件名</param>
public void deletefile(string strabsolutepath, string strfilename)
{
//判断路径最后有没有/符号,没有则自动加上
if(strabsolutepath.lastindexof("//") == strabsolutepath.length)
{
//判断要删除的文件是否存在
if(file.exists(strabsolutepath + strfilename))
{
//删除文件
file.delete(strabsolutepath + strfilename);
}
}
else
{
if(file.exists(strabsolutepath + "//" + strfilename))
{
file.delete(strabsolutepath + "//" + strfilename);
}
}
}
#endregion
#region 上传文件并返回文件名 savefile
/// <summary>
/// 上传文件并返回文件名
/// </summary>
/// <param name="hifile">htmlinputfile控件</param>
/// <param name="strabsolutepath">绝对路径.如:server.mappath(@"image/upload")与server.mappath(@"image/upload/")均可,用/符号亦可</param>
/// <returns>返回的文件名即上传后的文件名</returns>
public string savefile(htmlinputfile hifile,string strabsolutepath)
{
string stroldfilepath = "",strextension = "",strnewfilename = ""; //如果上传文件的文件名不为空
if(hifile.postedfile.filename != string.empty)
{
stroldfilepath = hifile.postedfile.filename;
//取得上传文件的扩展名
strextension = stroldfilepath.substring(stroldfilepath.lastindexof("."));
//文件上传后的命名
strnewfilename = getuniquestring() + strextension;
//如果路径末尾为/符号,则直接上传文件
if(strabsolutepath.lastindexof("//") == strabsolutepath.length)
{
hifile.postedfile.saveas(strabsolutepath + strnewfilename);
}
else
{
hifile.postedfile.saveas(strabsolutepath + "//" + strnewfilename);
}
}
return strnewfilename;
}
#endregion
#region 重新上传文件,删除原有文件coverfile
/// <summary>
/// 重新上传文件,删除原有文件
/// </summary>
/// <param name="fffile">htmlinputfile控件</param>
/// <param name="strabsolutepath">绝对路径.如:server.mappath(@"image/upload")与server.mappath(@"image/upload/")均可,用/符号亦可</param>
/// <param name="stroldfilename">旧文件名</param>
public void coverfile(htmlinputfile fffile,string strabsolutepath,string stroldfilename)
{
//获得新文件名
string strnewfilename = getuniquestring(); if(fffile.postedfile.filename != string.empty)
{
//旧图片不为空时先删除旧图片
if(stroldfilename != string.empty)
{
deletefile(strabsolutepath,stroldfilename);
}
savefile(fffile,strabsolutepath);
}
}
#endregion
c#.net 文件操作:上传 下载 删除 文件列表 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
以上就是在asp.net中上传下载文件实例代码的详细内容。