zoukankan      html  css  js  c++  java
  • FileUpload实现图片的无重上传

            //如果确认了上传文件,则判断文件类型是否符合要求
            if (this.FileUpload1.HasFile)
            {
                //获取上传文件的后缀
                String fileExtension = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();
                String[] restrictExtension = { ".gif", ".jpg", ".bmp", ".png" };
                //判断文件类型是否符合要求
                for (int i = 0; i < restrictExtension.Length; i++)
                {
                    if (fileExtension == restrictExtension[i])
                    {
                        fileIsValid = true;
                    }
                }
                //如果文件类型符合要求,调用SaveAs方法实现上传,并显示相关信息
                if (fileIsValid == true)
                {
                    try
                    {
                        String newname = Guid.NewGuid().ToString();//生成新的文件名,保证唯一性
                        this.Image1.ImageUrl = "~/images/head/" + newname+ FileUpload1.FileName;
                       
                        this.FileUpload1.SaveAs(Server.MapPath("~/images/head/") +newname+ FileUpload1.FileName);
                        this.Label1.Text = "文件上传成功";
                        this.Label1.Text += "<Br/>";
                        this.Label1.Text += "<li>" + "原文件路径:" + this.FileUpload1.PostedFile.FileName;
                        this.Label1.Text += "<Br/>";
                        this.Label1.Text += "<li>" + "文件大小:" + this.FileUpload1.PostedFile.ContentLength + "字节";
                        this.Label1.Text += "<Br/>";
                        this.Label1.Text += "<li>" + "文件类型:" + this.FileUpload1.PostedFile.ContentType;
                    }
                    catch (Exception ex)
                    {
                        this.Label2.Text = "无法上传文件" + ex.Message;
                    }
                }
                else
                {
                    this.Label2.Text = "只能够上传后缀为.gif,.jpg,.bmp,.png的文件夹";
                }
            }

  • 相关阅读:
    .net 读写记事本文件
    .net webservers的使用
    对象的复制
    .net form身份验证的配置
    JAVA反射机制
    Java6 脚本引擎(用Java调用JS脚本)调用js
    Ant教程
    JAVA关键字详解
    Java环境配制
    Java常用字符集编码详解
  • 原文地址:https://www.cnblogs.com/jqpy1994/p/6822017.html
Copyright © 2011-2022 走看看