当前位置: 首页 > news >正文

【转载】FileUpload控件如何实现上传和文件属性获取


        protected void btnUpload_Click(object sender, EventArgs e)

        {

【方法一】

            if (FileUpload1.PostedFile.FileName == "")
            {
                Label4.Text = "请选择文件!";
            }
            else
            {
                string filepath = FileUpload1.PostedFile.FileName;
                string filename = filepath.Substring(filepath.LastIndexOf("//") + 1);
                string serverpath = Server.MapPath("image/") + filename;
                FileUpload1.PostedFile.SaveAs(serverpath);
                Label4.Text = "上传成功!";
            }

            //***********************************

【方法二】

                //if (FileUpload1.HasFile)
                //{
                //    try
                //    {
                //        int startIndex = this.FileUpload1.FileName.LastIndexOf(@"\") + 1;
                //        string fileName = this.FileUpload1.FileName.Substring(startIndex);//文件名的获取
                //        string phyFileName = this.Server.MapPath("Image") + @"\" + fileName + ".bmp";
                //        this.FileUpload1.SaveAs(phyFileName);
                //        Label4.Text = "客户端路径:" + FileUpload1.PostedFile.FileName + "<br>" +
                //                  "文件名:" + System.IO.Path.GetFileName(FileUpload1.FileName) + "<br>" +
                //                  "文件扩展名:" + System.IO.Path.GetExtension(FileUpload1.FileName) + "<br>" +
                //                  "文件大小:" + FileUpload1.PostedFile.ContentLength + " KB<br>" +
                //                  "文件MIME类型:" + FileUpload1.PostedFile.ContentType + "<br>" +
                //                  "保存路径:" + Server.MapPath("upload") + "\\" + FileUpload1.FileName;
                //    }
                //    catch (System.Exception ex)
                //    {
                //        Label4.Text = "发生错误:" + ex.Message.ToString();
                //    }


                //}
                //else
                //{
                //    Label4.Text = "没有选择要上传的文件!";
                //    //message.AjaxResponeSrcipt(UpdatePanel1,this.GetType(),"提交成功");
                //}
            //} 

【方法三】
            //bool fileOK = false;
            //string path = Server.MapPath("Image");
            //if (FileUpload1.HasFile)
            //{
            //    String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            //    String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
            //    for (int i = 0; i < allowedExtensions.Length; i++)
            //    {
            //        if (fileExtension == allowedExtensions[i])
            //        {
            //            fileOK = true;
            //        }
            //    }
            //}
            //if (fileOK)
            //{
            //    try
            //    {
            //        FileUpload1.SaveAs(path + FileUpload1.FileName);
            //        Label4.Text = "文件上传成功.";
            //        Label4.Text = "<b>原文件路径:</b>" + FileUpload1.PostedFile.FileName + "<br />" +
            //            "<b>文件大小:</b>" + FileUpload1.PostedFile.ContentLength + "字节<br />" +
            //            "<b>文件类型:</b>" + FileUpload1.PostedFile.ContentType + "<br />";
            //    }
            //    catch (Exception ex)
            //    {
            //        Label4.Text = "文件上传不成功.";
            //    }
            //}
            //else
            //{
            //    Label4.Text = "只能够上传图片文件.";
            //}

        }

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

                        华丽丽的分割线

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

 if (fileUpload.HasFile)
            {
                try
                {
                    if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("Upload") + "\\" + strDirectory)) 

                     //判断现有磁盘是否存在目录文件夹是否存在                                                                                  
                    {
                        Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("UploadFiles") + "\\" + strDirectory);

                         //如果文件夹不存在,那么新建一个文件夹
                    }
                    fileUpload.SaveAs(System.Web.HttpContext.Current.Server.MapPath("UploadFiles") 
                                    + "\\" + strDirectory + "\\" 
                                    + Path.GetFileNameWithoutExtension(fileUpload.FileName) 

               }

http://www.xdnf.cn/news/858367.html

相关文章:

  • GPS巡检管理系统新思路-灵活、快捷、智能巡检系统解决方案_设备智能巡检系统,设备巡检系统,智能巡更管理系统,森林防护AI智能识别巡检系统,智慧工地智能巡检系统,数字化巡检系统,智能巡检管理系统
  • 搭建自己的个人服务器(网络配置篇)
  • Keil的软件仿真和硬件仿真
  • 写字楼管理系统软件哪款好
  • 五款上网行为管理软件,5款常用的电脑上网行为管理软件
  • 什么是Platform SDK
  • 日本の行政区画--都道府県
  • 运行Windows终端提示由于找不到mscoree.dll无法继续执行代码
  • 摄影常用词:光圈、快门、曝光、底片、ISO、焦距、景深
  • 分享50个免费的云盘网盘服务——拥有无限储存空间
  • Ubuntu20.04安装搜狗拼音(详细操作 有图)
  • Windows缺少d3dx9_34.dll文件解决
  • 国内十大破解论坛
  • 关于C语言中的Complex(复数类型)和imaginary(虚数类型)
  • ACDsee_14中文许可证秘钥
  • Ubuntu16.04 apache2配置虚拟主机
  • 局域网共享故障的分析与排除
  • FlashPlayer 11.4 安全性升级
  • Tomcat7启动报Error listenerStart错误
  • 4*4*4光立方制作分享
  • DM、PQ、PM、diskgen分区工具介绍比较
  • 计算机vf打开多个窗口知识,计算机二级VF知识点总结
  • 【主题词——玫瑰花】
  • 广告基础概念
  • 数据质量管理
  • cookie和session区别
  • linux grep -a命令,linux grep用法
  • 5款好用的轻量云服务器(618超便宜轻量云)
  • 2022保密教育线上培训考试 06
  • 适合国人的常用的Cydia源地址搜集