using System.IO;

 

////////////////////////////////上传代码开始
            Boolean fileOK = true;
            String path = Server.MapPath("../upFiles/");
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            String fname = fileUp.FileName;
            //判断文件是否已存在
            hetongTab = hetongAda.GetDataByFiles(fname);
            if (hetongTab.Rows.Count > 0)
            {
                addimg.Text = "文件已存在!";
                return;
            }
            if (fname != "")
            {
                //取得文件大小  
                int filesize = fileUp.PostedFile.ContentLength;
                if (filesize > (5120 * 1024) || filesize <= 0)
                {
                    //大于5120K或文件不存在时返回  
                    Response.Write("<script language='javascript'>alert('请确认文件存在且小于5M!');</script>");
                    return;
                }
            }
            else
            {
                addimg.Text = "没有文件!";
                return;
            }
            if (fileOK)
            {
                try
                {
                    //保存文件
                    fileUp.PostedFile.SaveAs(path + fname);
                }
                catch (Exception ex)
                {
                    addimg.Text = "文件操作失败";
                    return;
                }
            }
            ////////////////////////////////上传代码结束

 

 

------------------------------------------------------------------------------------

--------------------------------------图片上传-----------------------------------

protected void up_click(object sender, EventArgs e)
    {
        Boolean fileOK = false;
        String path = Server.MapPath("../uploadFile/");
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        String url = fileUp.FileName;
        if (url != "")
        {
            //取得文件大小   
            int filesize = fileUp.PostedFile.ContentLength;
            //Response.Write("文件大小:" + filesize+"<br>");
            if (filesize > (500 * 1024) || filesize <= 0)
            {
                //大于500K或文件不存在时返回   
                Response.Write("<script   language='javascript'>alert('请确认图片存在且小于500K!');</script>");
                return;
            }
        }
        else
        {
            Response.Write("<script>alert('没有图片!');</script>");
            return;
        }

        //获得文件扩展名
        String fileExtension =
            System.IO.Path.GetExtension(fileUp.FileName).ToLower();
        //判断文件扩展名是否符合条件
        if (fileUp.HasFile)
        {
            String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }
        //生成唯一文件名
        //方案一:利用GUID结构生成唯一文件名   
        Guid myGuid = Guid.NewGuid();
        string NewName = myGuid.ToString();
        NewName = NewName.Replace("-", "");
        NewName = NewName.ToUpper();
        //方案二:利用时间生成新文件名(年+月+日+小时+分钟+秒+毫秒)   
        //string   NewName   =   DateTime.Now.Year.ToString()   +   DateTime.Now.Month.ToString()   +   DateTime.Now.Day.ToString()   +   DateTime.Now.Hour.ToString()   +   DateTime.Now.Minute.ToString()   +   DateTime.Now.Second.ToString()   +   DateTime.Now.Millisecond.ToString();   
        ////方案三 使用文件原来名称作为文件名
        //string endFilename = url;
        //生成文件名
        string endFilename = NewName + fileExtension;
        if (fileOK)
        {
            try
            {
                //保存文件
                fileUp.PostedFile.SaveAs(path + endFilename);
                Response.Write("<script>window.returnValue='uploadFile/" + endFilename + "';window.close();</script>");
            }
            catch (Exception ex)
            {
                addimg.Text = "文件操作失败";
                return;
            }

        }
        else
        {
            addimg.Text = "<font color=red>文件格式不对.</font>";
            return;
        }


        //if (this.fileUp.HasFile)//判断是否有文件
        //{
        //    if (CheckFileType(fileUp.FileName))//检查上传文件的类型
        //    {
        //        string fileUpload = CMS.Util.WebConfigUtility.AppSetting("fileUpload");
        //        if (!Directory.Exists(CMS.Facade.Htm.Util.rootPath + fileUpload))
        //        {
        //            Directory.CreateDirectory(CMS.Facade.Htm.Util.rootPath + fileUpload);
        //        }
        //        string fileName = CMS.Util.StringUtility.GetNumberRandom() + System.IO.Path.GetExtension(fileUp.FileName);
        //        string filePath = fileUpload + "/" + fileName;
        //        String truePath = CMS.Facade.Htm.Util.rootPath + fileUpload + "\\" + fileName;
        //        fileUp.SaveAs(truePath);//把文件上传到服务器的绝对路径上
        //        this.msg.Text = "<script>window.returnValue='" + filePath + "';window.close();</script>";
        //    }
        //    else
        //    {
        //        this.msg.Text = "文件类型不正确!";
        //    }
        //}
    }
   
    //用来获取文件类型
    public bool CheckFileType(string fileName)
    {

        //获取文件的扩展名,前提要用这个方法必须引入命名空间io

        string ext = System.IO.Path.GetExtension(fileName);

        switch (ext.ToLower())
        {

            case ".gif":

                return true;

            case ".png":

                return true;

            case ".jpeg":

                return true;

            case ".jpg":

                return true;

            default:

                return false;

        }
    }

posted on 2013-11-13 17:00  御不凡  阅读(306)  评论(0编辑  收藏  举报