ASP.NET文件上传,为每个用户建立一个上传目录
2009-09-14 14:46
495 查看
文件上传界面既可以用Html的input file控件,又可以用FileUpload控件,只要将Html的input file控件加上runat="server",就会发现两者的功能完全是一模一样,上传的代码是共用的,不需要做任何改变。我想微软在将Html控件将成标准控件时应该只是多加了runat="server"吧。放入上述两者的任一控件后,添加一个标准的Button按钮(Html按钮也行,不过需要加上runat="server"),双击Button按钮,产生点击事件。在点击事件中写入以下代码:
首先检查是否已经选了文件
if (this.myFile.PostedFile != null)
{
检查文件根目录是否存在,不存在就要创建
if (!System.IO.Directory.Exists(Server.MapPath("~")+@"/photoes"))
{
System.IO.Directory.CreateDirectory(Server.MapPath("~")+@"/photoes");
}
此处Server.MapPath("~")用来表示项目根目录的物理路径。
接下来创建用户文件夹,根据用户ID创建
if(!System.IO.Directory.Exists(Server.MapPath("~")+@"/photoes/"+userID))
{
System.IO.Directory.CreateDirectory(Server.MapPath("~")+@"/photoes/"+userID)
}
string orignalName = this.myFile2.PostedFile.FileName;//获取客户机上传文件的路径
int lastdotlocation = orignalName.LastIndexOf(".");
string extendName = orignalName.Substring(lastdotlocation);//获取扩展名
if (extendName != ".gif" && extendName != ".jpg" && extendName != ".jpeg" && extendName != ".png")
{
Response.Write("Wrong format");
Response.End();
}//检查文件格式
string newName = DateTime.Now.Millisecond.ToString() + "_" + myFile2.PostedFile.ContentLength.ToString() + extendName;//对文件进行重命名
myFile.PostedFile.SaveAs(Server.MapPath("~") + @"/photoes/" +userID+@"/"+ newName);
}
首先检查是否已经选了文件
if (this.myFile.PostedFile != null)
{
检查文件根目录是否存在,不存在就要创建
if (!System.IO.Directory.Exists(Server.MapPath("~")+@"/photoes"))
{
System.IO.Directory.CreateDirectory(Server.MapPath("~")+@"/photoes");
}
此处Server.MapPath("~")用来表示项目根目录的物理路径。
接下来创建用户文件夹,根据用户ID创建
if(!System.IO.Directory.Exists(Server.MapPath("~")+@"/photoes/"+userID))
{
System.IO.Directory.CreateDirectory(Server.MapPath("~")+@"/photoes/"+userID)
}
string orignalName = this.myFile2.PostedFile.FileName;//获取客户机上传文件的路径
int lastdotlocation = orignalName.LastIndexOf(".");
string extendName = orignalName.Substring(lastdotlocation);//获取扩展名
if (extendName != ".gif" && extendName != ".jpg" && extendName != ".jpeg" && extendName != ".png")
{
Response.Write("Wrong format");
Response.End();
}//检查文件格式
string newName = DateTime.Now.Millisecond.ToString() + "_" + myFile2.PostedFile.ContentLength.ToString() + extendName;//对文件进行重命名
myFile.PostedFile.SaveAs(Server.MapPath("~") + @"/photoes/" +userID+@"/"+ newName);
}
相关文章推荐
- ASP.NET文件上传,为每个用户建立一个上传目录
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- 用asp.net mvc写一个后台文件上传
- ASP.NET WebApi 上传文件时异常 Failed to execute send on XMLHttpRequest 的一个处理方法
- 在asp.net网站下使用fckeditor 和fcfinder (包括修改fcfinder 来使上传文件按时间来命名和按用户分割文件)
- Asp.netUpload(大文件上传) 终于找到一个可以用的了
- [导入]在asp.net中利用FileUplad控件从同一个页面上传多个文件
- Scott Mitchell 的ASP.NET 2.0数据教程之56:添加新记录时包含一个文件上传选项
- cuteeditor 为每个用户定义一个上传目录
- ASP.NET 中将文件上传到另外一个服务器的方法二
- 在ASP.NET 2.0中操作数据之五十四:添加新记录时包含一个文件上传选项
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- ASP.NET 中将文件上传到另外一个服务器的方法
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件
- ASP.NET MVC 重点教程一周年版 第十一回 母版页、用户自定义控件及文件上传
- 在Asp.net中如何遍历一个本地目录下的所有文件并上载到Web 服务器上指定的目录中
- FCKeditor是使用非常广泛的HTML编辑器,本文从 ASP.NET 的使用场景对 FCKeditor 与 FCKeditor.NET 的配置、功能扩展(如自定义文件上传子目录、自定义文件名、上传图片的后期处理等)、以及安全性进行初步的阐述。
- ASP.NET MVC 重点教程一周年版 第十一回 母版页、用户自定义控件及文件上传
- ASP.NET中上传EXCEL文件一个比较快的方法