net上传文件并创建文件夹
2010-12-17 16:39
267 查看
代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 上传文件到指定的文件夹
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
//根据年份判断在该路径下是否存在以当年年份文件夹 否则将建立以该年份的文件夹
if (!Directory.Exists(HttpContext.Current.Server.MapPath("photos/") + "\\" + classid)) //HttpContext.Current.Server.MapPath(相对路径):把相对路径转为服务器上的绝对路径。 File.Exists(绝对路径):检查是否存在绝对路径指向的文件或目录。
{
System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("photos/") + "\\" + classid); //System.IO.Directory.CreateDirectory(文件夹绝对路径):建立绝对路径文件夹。
}
string photoName2 = now.Millisecond.ToString() + "_" + myFile.PostedFile.ContentLength.ToString() + newext; //重新为文件命名,时间毫秒部分+文件大小+扩展名
myFile.PostedFile.SaveAs(Server.MapPath("photos\\"+classid+"\\" + photoName2)); // 保存文件到路径,用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 上传文件到指定的文件夹
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
//根据年份判断在该路径下是否存在以当年年份文件夹 否则将建立以该年份的文件夹
if (!Directory.Exists(HttpContext.Current.Server.MapPath("photos/") + "\\" + classid)) //HttpContext.Current.Server.MapPath(相对路径):把相对路径转为服务器上的绝对路径。 File.Exists(绝对路径):检查是否存在绝对路径指向的文件或目录。
{
System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("photos/") + "\\" + classid); //System.IO.Directory.CreateDirectory(文件夹绝对路径):建立绝对路径文件夹。
}
string photoName2 = now.Millisecond.ToString() + "_" + myFile.PostedFile.ContentLength.ToString() + newext; //重新为文件命名,时间毫秒部分+文件大小+扩展名
myFile.PostedFile.SaveAs(Server.MapPath("photos\\"+classid+"\\" + photoName2)); // 保存文件到路径,用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
}
}
相关文章推荐
- asp.net ftp 上传,下载,删除文件,创建文件夹,删除文夹,重命名类
- 使用edtftpnet上传文件到Serv-U出现乱码的问题,并且不能创建中文文件名或文件夹。
- asp.net上传文件并创建文件夹和删除文件
- asp.net上传文件与创建文件夹实例
- ASP.NET真的进度条,上传文件和文件夹
- php 生成自动创建文件夹并上传文件的示例代码
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
- VS2017创建 ASP.NET Core 1.1 Web项目,上传大文件时提示404
- ASP.NET(c#)FTP 上传文件文件夹【递归上传】
- asp.net下CKFinder IE浏览器中上传图片文件时提示“无效文件名或文件夹名称”的解决方法(原创文章)
- iOS文件处理,将图片存到手机目录里面 创建文件夹,NSFileManager用来存储图片,然后遍历将图片取出上传到服务器哦!!
- .net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等
- asp.net 文件夹和文件的创建、删除
- asp.net下CKFinder IE9以下浏览器中上传图片文件时提示“无效文件名或文件夹名称”的解决方法
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
- C#.net获取指定文件夹下的所有jpg文件,并按文件创建时间排序
- Asp.net上传控件上传文件到文件夹
- [sharepoint]rest api文档库文件上传,下载,拷贝,剪切,删除文件,创建文件夹,修改文件夹属性,删除文件夹,获取文档列表
- asp.net文件操作小例子(创建文件夹,读,写,删)
- net,C#,Ftp各种操作,上传,下载,删除文件,创建目录,删除目录,获得文件列表等