文件上传是一个很实用的功能,在asp.net2.0中,文件上传很容易就实现了.文件上传主要用到FileUpload(详细内容参看msdn),下面通过一个例子介绍FileUpload的常用的属性以及方法.这个例子的功能是实现文件的本地上传以及根据当前时间对上传文件重命名还有实现自己创建上传目录.
代码:

if (fileupload1.HasFile)//判断文件是否为空
{

string vsfullname = fileupload1.PostedFile.FileName;//获取文件的名称包含路径,此处没有用到它
string vsfilename =fileupload1.FileName;//获取文件的名称
int index = vsfilename.LastIndexOf(".");
string vstype = vsfilename.Substring(index).ToLower();//取文件的扩展名
string vsnewname = System.DateTime.Now.ToString("yyyyMMddHHmmssffff");//声称文件名,防止重复
vsnewname = vsnewname + vstype;//完整的上传文件名
//string fullpath = Server.MapPath("~/" + TextBox1.Text.Trim() + "/");//这样就可以实现自己创建文件夹
string fullpath=Server.MapPath("~/image/");//文件的上传路径
if(!Directory.Exists(fullpath))//判断上传文件夹是否存在,若不存在,则创建
{//这个地方可以做成自己创建文件夹
Directory.CreateDirectory(fullpath);//创建文件夹
// string vsurl=Server.MapPath("~/" + TextBox1.Text.Trim() + "/")+vsnewname;
string vsurl=Server.MapPath("~/image/")+vsnewname;
fileupload1.SaveAs(vsurl);
}

相关文章:

  • 2022-12-23
  • 2021-09-28
  • 2021-04-27
  • 2021-12-20
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-01
  • 2022-12-23
  • 2021-06-13
  • 2021-09-16
  • 2021-11-05
  • 2021-09-06
相关资源
相似解决方案