C#上传文件,读取文件夹和文件
using System.IO;
Protected void ReadFile() //读取文件夹,文件
...{
string savePath = @"PackNetProductIMG";
string absSavePath = Server.MapPath(savePath);
string[] Directorys = Directory.GetDirectories(absSavePath + @"/" + Page.User.Identity.Name);
for (int i = 0; i < Directorys.Length; i++)
...{
Response.Write(Directorys[i] + " <br/ >");
}
Response.Write("Directorys count:" + Directorys.Length + "个 <br/ >--------- <br >");
string[] Files = Directory.GetFiles(absSavePath + @"" + Page.User.Identity.Name, "*");
for (int i = 0; i < Files.Length; i++)
...{
Response.Write(Files[i] + " <br/ >");
}
Response.Write("Files count:" + Files.Length + "个 <br/ >--------- <br >");
}
protected void Button1_Click(object sender, EventArgs e) //上传图片
...{
string savePath = "/PackNet/ProductIMG";
string absSavePath = Server.MapPath(savePath);
if (!Directory.Exists(absSavePath))
Directory.CreateDirectory(absSavePath);
if (!Directory.Exists(absSavePath + @"" + Page.User.Identity.Name))
Directory.CreateDirectory(absSavePath + @"" + Page.User.Identity.Name);
if (this.PicFile.PostedFile.ContentLength < 100)
...{
Response.Write("<script>alert('图片太小!!');</script>");
return;
}
if (this.PicFile.PostedFile.ContentLength > 500000)
...{
Response.Write("<script>alert('图片太大!!');</script>");
return;
}
string clientFileName = this.PicFile.PostedFile.FileName;
string exName = clientFileName.Substring(clientFileName.LastIndexOf('.')).ToLower();
if (exName != ".bmp" && exName != ".jpg" && exName != ".gif")
...{
Response.Write("<script>alert('文件格式错误!!');</script>");
return;
}
string fileName = Guid.NewGuid().ToString();
fileName += exName;
this.PicFile.PostedFile.SaveAs(absSavePath + @"/" + Page.User.Identity.Name + @"/" + fileName);
}
文章出处:http://www.diybl.com/course/4_webprogram/asp.net/netjs/20071020/78354.html