C#实现Web文件上传

来源:互联网 发布:c语言画生日蛋糕 编辑:程序博客网 时间:2024/04/26 21:03

一、前台页面:           

 <form runat="server">

                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" />
                <br />
                <br />
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                <br />
                <asp:Image ID="Image1" runat="server" Visible="False" />
                <asp:Label ID="fileUrl" runat="server" Text="Label" Visible="false"></asp:Label>
                <asp:Label ID="fileName" runat="server" Text="Label" Visible="false"></asp:Label>
                <asp:Label ID="filesize" runat="server" Text="Label" Visible="false"></asp:Label>
                <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

            </form>

二、后台代码:

 protected void Button1_Click(object sender, EventArgs e)
        {
            bool fileOK = true;


            //文件的上传路径
            string path = Server.MapPath("Files\\");


            //判断上传文件夹是否存在,若不存在,则创建
            if (!Directory.Exists(path))
            {
                //创建文件夹 
                Directory.CreateDirectory(path);
            }
            if (FileUpload1.HasFile)
            {


                //允许上传的类型
                //string[] allowExtesions = { ".doc", ".xls", ".rar", ".zip", ".ppt" };
                //for (int i = 0; i < allowExtesions.Length; i++)
                //{
                //    if (fileExtesion == allowExtesions[i])
                //    {
                //        //文件格式正确 允许上传
                //        fileOK = true;
                //    }
                //}


                string name = FileUpload1.FileName;// 获得上传文件的名字.
                int size = FileUpload1.PostedFile.ContentLength;// 文件大小.
                if (size > (10 * 1024))
                {
                    fileOK = false;
                    Label2.Text = "文件过大";
                }
                string type = FileUpload1.PostedFile.ContentType;// 文件类型.


                // 获取上传文件的类型(后缀)
                string fileExtesion = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();


                //string type2 = name.Substring(name.LastIndexOf(".") + 1);// LastIndexOf()最后一个索引位置匹配.Substring()里面的+1是重载.
                if (fileOK)
                {
                    try
                    {
                        Random ranNum = new Random();


                        // 在1和1000之间的随机正整数
                        int num = ranNum.Next(1, 1000);


                        // 获取当前时间
                        string newname = System.DateTime.Now.ToString("yyyyMMddHHmmssffff");


                        // 声明文件名,防止重复
                        newname = newname + num + fileExtesion;


                        string ipath = Server.MapPath("Files\\upimg") + "\\" + newname; // 取得根目录下面的upimg目录的路径.
                        string fpath = Server.MapPath("Files\\upfile") + "\\" + newname;
                        string wpath = "Files\\upimg\\" + newname; // 获得虚拟路径
                        if (fileExtesion == ".jpg" || fileExtesion == ".gif" || fileExtesion == ".bmp" || fileExtesion == ".png")
                        {
                            FileUpload1.SaveAs(ipath); // 保存方法,参数是一个地址字符串.
                            Image1.ImageUrl = wpath;
                            Label1.Text = "你传的文件名是:" + name + "<br>文件大小为:" + size + "字节<br>文件类型是:" + type +
                                "<br>后缀是:" + fileExtesion + "<br>实际路径是:" + ipath + "<br>虚拟路径是:" + fpath;
                            Image1.Visible = true;
                            fileUrl.Text = ipath;
                        }
                        else
                        {
                            Image1.Visible = false;
                            FileUpload1.SaveAs(fpath);
                            Label1.Text = "你传的文件名是:" + name + "<br>文件大小为:" + size + "字节<br>文件类型是:" + type +
                                "<br>后缀是:" + fileExtesion + "<br>实际路径是:" + ipath + "<br>虚拟路径是:" + fpath;
                            fileUrl.Text = fpath;
                        }
                        // FileUpload1.PostedFile.SaveAs(path + newname);


                        //Session["filename"] = newname;
                        Label2.Text = "上传成功";
                        fileName.Text = name;
                        filesize.Text = size.ToString();
                        //lab_upload.Text = "上传成功";
                    }
                    catch (Exception ex)
                    {


                        Label2.Text = "上传失败";
                        throw ex;
                    }
                }
            }
            else
            {
                //尚未选择文件
                Label2.Text = "尚未选择任何文件,请选择文件";
                return;
            }
        }

0 0
原创粉丝点击