upload

 public partial class mail_upload : System.Web.UI.Page
    {
        public string UploadFileExt = ".asp|.sql";
        protected long uploadFileSize = 3027;
        string _tmps = string.Empty;
        string session = string.Empty;
        string strValue = string.Empty;

        protected void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            AjaxPro.Utility.RegisterTypeForAjax(typeof(mail_upload));
            //Session["sysuserid"]="10000";
            session = Session["sysuserid"].ToString();
            if (!Page.IsPostBack)
            {
                getFile();
            }
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.Read)]
        public DataTable GetFiles()
        {
            return Sessioner.FILES;
        }

        private void getFile()
        {
            if (Sessioner.FILES != null)
            {
                for (int i = 0; i < Sessioner.FILES.Rows.Count; i++)
                {
                    HtmlTableRow tr = new HtmlTableRow();
                    tr.Align = "center";
                    tr.Attributes.Add("style", "height=30");

                    HtmlTableCell tc1 = new HtmlTableCell();
                    tc1.InnerText = Convert.ToString(i + 1);
                    tc1.Width = "10%";
                    tr.Cells.Add(tc1);

                    HtmlTableCell tc2 = new HtmlTableCell();
                    tc2.InnerText = Sessioner.FILES.Rows[i]["NAME"].ToString();
                    tc2.Width = "75%";
                    tr.Cells.Add(tc2);

                    HtmlTableCell tc3 = new HtmlTableCell();
                    tc3.InnerHtml = "<input type='button' class='btn' value='删除' οnclick='DelData(" + i + ")'>";
                    tc3.Width = "15%";
                    tr.Cells.Add(tc3);
                    tbFile.Rows.Add(tr);
                }
            }

        }

 

        protected void btn_Upload_Click(object sender, System.EventArgs e)
        {
            string path = "../../Upload/mail/" + session + "/";
            string fileName = string.Empty;
            string fullName = string.Empty;
            string fileExtension = string.Empty;

            fullName = fileToUpload.PostedFile.FileName;
            fileName = Path.GetFileName(fullName);
            fileExtension = Path.GetFileNameWithoutExtension(fullName);

            if (fileName != "" && chkfile(fileToUpload.PostedFile.ContentLength, fileExtension))
            {
                if (!Directory.Exists(Server.MapPath(path)))
                {
                    CreateDir(path);
                }
                if (File.Exists(Server.MapPath(path + fileName)))//重名
                {
                    fileName = Path.GetFileNameWithoutExtension(fullName);
                    fileExtension = Path.GetExtension(fullName);
                    fileName = fileName + "1" + fileExtension;
                }
                fileToUpload.PostedFile.SaveAs(Server.MapPath(path + fileName));
                SaveFiles(fileName);
                getFile();

            }
            else if (fileName == "")
            {

                Common.MessageBox.Show(this, "请选择你要上传的文件!");
                return;
            }
            else
            {
                Common.MessageBox.Show(this, "不支持的文件格式或文件大小超过了3M的限制");
                return;
            }
            if (Session["from"] == null)
            {
                Response.Write("<script language='javascript'>\n\twindow.opener.document.getElementById(\"lb_fileupload\").innerText=\"" + FileName() + "\"\n</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>\n\ttop.document.getElementById(\"lb_fileupload\").innerText=\"" + FileName() + "\"\n</script>");
            }
        }

        private void SaveFiles(string filename)
        {
            DataRow row = Sessioner.FILES.NewRow();

            row["NAME"] = filename;
            Sessioner.FILES.Rows.Add(row);
        }
        private string FileName()
        {
            string name = string.Empty;
            if (Sessioner.FILES != null)
            {
                for (int i = 0; i < Sessioner.FILES.Rows.Count; i++)
                {
                    name += Sessioner.FILES.Rows[i]["NAME"].ToString() + ";";
                }
            }
            Session["filenames"] = name;
            return name;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public void Delete_Row(int row)
        {
            Sessioner.FILES.Rows.RemoveAt(row);
            Common.FileUpload.checkmail(this, Session["sysuserid"].ToString(), Session["filenames"].ToString());//BUG--上传文件临时删除之后文件未作删除。 
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public void Delete_FileName()
        {
            string name = string.Empty;
            if (Sessioner.FILES != null)
            {
                for (int i = 0; i < Sessioner.FILES.Rows.Count; i++)
                {
                    name += Sessioner.FILES.Rows[i]["NAME"].ToString() + ";";
                }
            }
            Response.Write("<script language='javascript'>\n\twindow.opener.document.getElementById(\"lb_fileupload\").innerText=\"" + name + "\"\n</script>");
        }

        /// <summary>
        /// check上传文件是否符合上传要求
        /// </summary>
        private bool chkfile(long contentLength, string fileExt)
        {
            if (("|" + UploadFileExt + "|").IndexOf(("|" + fileExt + "|")) > 0)
            {
                return false;
            }

            //检测文件大小
            if (contentLength < 1 || contentLength > uploadFileSize * 1024)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        private void CreateDir(string uploadDir)
        {

            if (!uploadDir.EndsWith("/")) uploadDir += "/";
            string tempPath = "";
            string tempMapPath = "";
            string tempDir = uploadDir;//uploadDir.Substring(0,2)=="~/" ? uploadDir : uploadDir.Insert(0,"~/") ;
            while (tempDir.IndexOf("/") >= 0)
            {
                tempPath += tempDir.Substring(0, tempDir.IndexOf("/") + 1);
                tempMapPath = MapPath(tempPath);
                if (!Directory.Exists(tempMapPath))
                {
                    Directory.CreateDirectory(tempMapPath);
                }
                tempDir = tempDir.Substring(tempDir.IndexOf("/") + 1);
            }
        }

 

 

 

 string fileName = System.IO.Path.GetFileName(fileUploadBox.Value);
            string extension = System.IO.Path.GetExtension(fileName).ToLower();

            DeleteFileByUserID();

            if (extension != ".dbf" && extension != ".xls")
            {
                return;
            }
            string userID = HttpContext.Current.User.Identity.Name;
            string saveFileName = String.Empty;
            if (extension == ".dbf")
            {
                saveFileName = "D" + userID + "_" + GenerateRandomFilename(4) + extension;
            }
            if (extension == ".xls")
            {
                saveFileName = "X" + userID + "_" + GenerateRandomFilename(8) + extension;
            }

            int fileSize = fileUploadBox.PostedFile.ContentLength;
            string maxRequestLength = System.Configuration.ConfigurationSettings.AppSettings["maxRequestLength"];

            if (maxRequestLength == null || maxRequestLength == String.Empty)
            {
                maxRequestLength = "20480";
            }

            if (fileSize > (int.Parse(maxRequestLength) * 1024) || fileSize <= 0)
            {
                string msg = "<script>alert('附件大小不能大于" + int.Parse(maxRequestLength) / 1024 + "M,附件不能上传!')

</script>";
                this.liScript.Text = msg;
            }

            HttpPostedFile postedFile = fileUploadBox.PostedFile;
            string directory = Server.MapPath("~\\stuManager\\" + saveFileName);
            try
            {
                postedFile.SaveAs(directory);
                this.tbSourceFileName.Text = saveFileName;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Write(ex.Message);
                DeleteFileByUserID();
                this.tbSourceFileName.Text = String.Empty;
                this.liScript.Text = "<script>alert('上传文件出错,请重试一次!');</script>";

 

 

 string path = "";
        if (RadioButtonList1.SelectedItem.Value == "1")
            path = "stuPhoto";
        else
            path = "stuPhotoNew";

        if (this.FileUpload1.HasFile == false)
        {
            Response.Write("<script>alert('请先选择要上传的试卷!')</script>");
            return;
        }
        string f = FileUpload1.PostedFile.FileName;
        try
        {
            string fname = DateTime.Now.Ticks.ToString(); //生成文件名

            string FileName = fname + Path.GetExtension(f);
            this.FileUpload1.PostedFile.SaveAs(Server.MapPath("../" + path + "\\" + FileName));
            if (UnRAR(Server.MapPath("../" + path + "\\" + FileName), Server.MapPath("../" + path + "\\")))
            {
                File.Delete(Server.MapPath("../" + path + "\\" + FileName));
                Response.Write("<script>alert('文件上传成功!');location.href='phototype.aspx';</script>");
              
            }
        }
        catch
        {
            Response.Write("<script>alert('文件上传失败')</script>");
            return;
        }                 return;
            }

            this.InitSourceCols(directory);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值