zoukankan      html  css  js  c++  java
  • 图片上传

    protected void Upload()
            {
                //判断是否选择文件
                if (FileUpload1.HasFile)
                {
                    string fileContentType = FileUpload1.PostedFile.ContentType;//获取文件类型
                    //判断类型是否符合条件
                    if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg"||fileContentType=="image/PNG")
                    {
                        //客户端文件路径
                        string name = FileUpload1.PostedFile.FileName;
                        FileInfo file = new FileInfo(name);
                        //文件名称
                        string fileName = file.Name;
                        //服务器端文件路径
                        string webFilePath = Server.MapPath("resource/" + fileName);
                        //判断同名文件是否存在
                        if (!File.Exists(webFilePath))
                        {
                            try
                            {
                                //使用SaveAs方法保存文件
                                FileUpload1.SaveAs(webFilePath);
                                this.lMsg.Text = "提示:文件" + fileName + "上传成功!";
                                this.lPathInfo.Text = "resource" + fileName;
                                this.iPic.ImageUrl = "resource" + fileName;
                            }
                            catch (Exception ex)
                            {
                                this.lMsg.Text = "提示:文件上传失败,失败原因:" + ex.Message;
                            }
                        }
                        else
                        {
                            this.lMsg.Text = "提示:文件已经存在,请重命名后上传";
                        }
                    }
                    else
                    {
                        this.lMsg.Text = "提示:文件类型不符合";
                    }
                }
            }
            protected void btUpload_Click(object sender, EventArgs e)
            {
                if (Directory.Exists("resource"))
                {
                    Upload();
                }
                else
                {
                   
                    Directory.CreateDirectory(Server.MapPath("resource"));
                    Upload();
                }
               
            }
    感谢来访,共同学习!
  • 相关阅读:
    人为什么会生气 --- 答案是什么?
    职场中我们常犯的8个错误
    职场上最常见的20条错误,犯三条就够致命啦
    C语言,基于单向链表实现,变长动态数据缓冲区(线程安全) ---- 类似java的StringBuffer --- 亲测OK
    门限签名
    基于RSA的实用门限签名算法
    图解密码技术(第3版)-第4章
    各种加密算法比较
    密码那点事儿
    数字签名,我有疑问。
  • 原文地址:https://www.cnblogs.com/dingxiaowei/p/3058837.html
Copyright © 2011-2022 走看看