@流程

  1,前台代码:

  <asp:FileUpload ID="upload_img" runat="server" /><asp:Button ID="Button1" runat="server" Text="发布产品" OnClick="Button1_Click" />

  2,后台代码:

  protected void Button1_Click(object sender, EventArgs e)
    {
        string strName = upload_img.PostedFile.FileName;//使用fileupload控件获取上传文件的文件名
        string title = Request.Form["title"];   //获取图片标题,click事件会触发submit事件,表单会提交
        if (!string.IsNullOrEmpty(title))       
        {
            if (strName != "") //如果文件名存在
            {
                bool fileOK = false;
                int i = strName.LastIndexOf("."); //获取。的索引顺序号,在这里。代表图片名字与后缀的间隔
                string kzm = strName.Substring(i);
                    //获取文件扩展名的另一种方法 string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                string newName = Guid.NewGuid().ToString(); //生成新的文件名,保证唯一性

                string xiangdui = @"~\Admin\goods_img"; //设置文件相对网站根目录的保存路径 ,~号表示网站根目录,在此表示根目录下的images文件夹
                string juedui = Server.MapPath("~\\Admin\\goods_img\\");
                    //设置文件保存的本地目录绝对路径,对于路径中的字符“\”在字符串中必须以“\\”表示,因为“\”为特殊字符。或者可以使用上一行的给路径前面加上@
                string newFileName = juedui + newName + kzm;
                if (upload_img.HasFile) //验证 FileUpload 控件确实包含文件
                {

                    String[] allowedExtensions = {".gif", ".png", ".bmp", ".jpg"};
                    for (int j = 0; j < allowedExtensions.Length; j++)
                    {
                        if (kzm.ToLower() == allowedExtensions[j])
                        {
                            fileOK = true;
                        }
                    }
                }
                if (fileOK)
                {
                    try
                    {
                        // 判定该路径是否存在
                        if (!Directory.Exists(juedui))
                            Directory.CreateDirectory(juedui);     
                        //Label1.Text = newFileName; //为了能看清楚我们提取出来的图片地址,在这使用label
                        //Label2.Text = "<b>原文件路径:</b>" + upload_img.PostedFile.FileName + "<br />" +
                        //              "<b>文件大小:</b>" + upload_img.PostedFile.ContentLength + "字节<br />" +
                        //              "<b>文件类型:</b>" + upload_img.PostedFile.ContentType + "<br />";
                        //Label3.Text = xiangdui + newName + kzm;
                        //Label4.Text = "文件上传成功.";
                        upload_img.PostedFile.SaveAs(newFileName); //将图片存储到服务器上
                        if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(newFileName))
                        {
                            int result = DAL.insert_goods(title, newName+kzm);
                            if (result == 1)
                            {
                                alertMsg.Text = "<script>alert('发布成功!');</script>";
                            }
                            else
                            {
                                alertMsg.Text = "<script>alert('发布失败!');</script>";
                            }
                        }
                    }
                    catch (Exception)
                    {
                        alertMsg.Text = "<script>alert('文件上传失败!');</script>";
                    }
                }
                else
                {
                    alertMsg.Text = "<script>alert('只能够上传图片文件!');</script>";
                }
            }
            else
            {
                alertMsg.Text = "<script>alert('请选择上传图片!');</script>";
            }
        }
        else
        {
            alertMsg.Text = "<script>alert('请输入标题!');</script>";
        }
    } 

 

@常见问题

  1,Asp.net上传出现“超过了最大请求长度”的问题解决方法?

  在<configuration><system.web>下的<httpRuntime>标签中,添加maxRequestLength="502400"就可以了。

posted on 2015-02-03 16:38  学到老死  阅读(261)  评论(0)    收藏  举报