日期:2014-05-18 浏览次数:20533 次
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Text; using System.IO; using Asiastar.NRModel; using Asiastar.NRBLL; namespace Asiastar.NR.FileOperation { public partial class UpLoad : System.Web.UI.Page { public string method = ""; public string FolderId = ""; //public string FileTypeImg = ""; protected void Page_Load(object sender, EventArgs e) { FolderId = Request["id"].ToString(); /*判断用户是否登录*/ if (Session["username"] == null) { Response.Redirect("Default.aspx"); } /*判断form表单中的按钮是否被点击*/ method = Request.Form["method"]; if (!string.IsNullOrEmpty(method) && method.Equals("Upload")) { FN_UpFiles();//调用函数 } } #region 上传文件到数据库和服务器 public void FN_UpFiles() { //遍历File表单元素 HttpFileCollection files = HttpContext.Current.Request.Files; try { for (int iFile = 0; iFile < files.Count; iFile++) { //检查文件扩展名字 HttpPostedFile postedFile = files[iFile]; string fileName = "";//定义文件名 //string fileExtension = ""; fileName = Path.GetFileName(postedFile.FileName);//得到上传文件的完整名称 即文件名+后缀名 int index = fileName.IndexOf("."); string FileType = fileName.Substring(index).ToLower();//截取文件后缀名 //FileTypeImg = "../FileTypeimg/" + hz + ".gif"; Guid fileGuid = Guid.NewGuid();//生成新的文件名称 以GUID命名防止文件名相同 string NewFileName = fileGuid.ToString();//新的文件名 NewFileName = NewFileName + FileType;//新的文件名+后缀名 if (fileName != "")//如果文件名不为空 { try { //文件虚拟路径 string strpath = System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName; try { NRModel.File model = new NRModel.File(); NRBLL.File bf = new NRBLL.File(); Guid guid1 = Guid.NewGuid(); Guid guid2 = new Guid(FolderId); Guid guid3 = Guid.NewGuid(); Guid guid4 = Guid.NewGuid(); model.Fileid = guid1; model.Folderid = guid2; model.Filepath = strpath; model.FileNam = fileName; model.FileSize = postedF