protected void UpLoadFile_Click(Object sender, EventArgs e)
    {
       
            string path = Server.MapPath("./") + "image/case/";//获取当前目录下upfile文件夹在服务器上的绝对地址,作为图片保存地址
            if (UpLoadFile.HasFile)//判断本地需要上传的文件是否已经选取
            {
                string fileExt = System.IO.Path.GetExtension(UpLoadFile.FileName).ToLower();//获取上传文件的扩展名并转换为小写,为下面的上传类型合法性判断做准备
                if (fileExt == ".gif" || fileExt == ".jpg")//只允许上传gif和jpg的文件格式
                {
                    //开始执行上传操作
                    try
                    {
                        UpLoadFile.SaveAs(path + UpLoadFile.FileName);//服务端保存该文件
                        FileDetail.Visible = true;//显示上传文件的信息
                        //显示上传文件的名称,类型和大小
                        FileName.InnerHtml = UpLoadFile.PostedFile.FileName;
                        FileType.InnerHtml = FileName.InnerHtml.Substring(FileName.InnerHtml.LastIndexOf(".")).ToString().Trim();
                        FileSize.InnerHtml = (UpLoadFile.PostedFile.ContentLength / 1024) + "KB";
                        SaveURL.InnerHtml = path + UpLoadFile.FileName;//显示保存到服务器端文件的路径
                        Image1.Visible = true;
                        Image1.ImageUrl = "image/case/" + UpLoadFile.FileName;
                        string name = UpLoadFile.FileName.ToString();
                        Session["photo2"] = name;
                    }
                    catch (Exception ex)
                    {                        Literal lt = new Literal();
                        lt.Text = "<script>alert('文件上传过程发生错误,错误信息: '" + ex.Message + "'')</script>";
                        this.Page.Controls.Add(lt);                    }
                }
                else
                {                    Literal li = new Literal();
                    li.Text = "<script>alert('程序只支持上传.gif或jpg格式的文件')</script>";
                    this.Page.Controls.Add(li);
                }
            }
            else
            {//如果没有选择本地文件
                Literal l2 = new Literal();
                l2.Text = "<script>alert('请先选择文件')</script>";
                this.Page.Controls.Add(l2);
            }
        
    }

解决方案 »

  1.   

    UpLoadFile.SaveAs(path + System.IO.Path.GetFileName(UpLoadFile.FileName));Image1.ImageUrl = "image/case/" + System.IO.Path.GetFileName(UpLoadFile.FileName);
      

  2.   

    下面这是报的错误:
    Server Error in '/' Application.
    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error:An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
      

  3.   

    这种写法我也是看别人写的,不知道这句话是什么意思,感觉是就这句话报错了
    “string path = Server.MapPath("./") + "image/case/";//获取当前目录下upfile文件夹在服务器上的绝对地址,作为图片保存地址”
    这其中的“Server.MapPath("./")”
      

  4.   

    建议使用
    Server.MapPath("~/upfile/image/case/")
    等这样的绝对根路径
    另外string path = Server.MapPath("./") + "image/case/"也是错误的,应该是
    string path = Server.MapPath("./") + "image\\case\\" 这样的物理路径
      

  5.   

    System.NullReferenceException
    说明有对象为null了。你需要进行检查
      

  6.   

    if (UpLoadFile.HasFile )这个出错了
    如果你上传个文件,内容为空,这个是false
      

  7.   

    我的form标签是这样的:
    <form id="Form1" runat="server" method="post" enctype="multipart/form-data"></form>
    会不会有什么影响呢?