<asp:FileUpload ID="FileUpload1" runat="server" />在vs2008中rj提交上传就显示无法访问,没有执行事件就没了

解决方案 »

  1.   

         /// </summary>
        /// <param name="FileUpload1">FileUpload上传控件</param>
        /// <param name="filesize">自定义的要上传文件的最大大小</param>
        /// <param name="CurrFileSize">上传文件的实际大小</param>
        /// <param name="CurrFileType">上传文件的类型</param
        /// <param name="types">参数(0为只上传图片,1只能上传视频和Flash)</param>
        /// <returns>返回一个ArrayList类(文件保存库的相对路径,文件的大小和文件类型)</returns>
        public static ArrayList UploadFile(System.Web.UI.WebControls.FileUpload FileUpload1, int filesize, string Pathfile, int types)
        {
            ArrayList list = new ArrayList();
            int filetype = 0;
            string CurrFileSize = "";
            string CurrFileType = "";
            if (FileUpload1.FileName != "")
            {
                Page pages = new Page();
                string name = FileUpload1.FileName;//文件名称
                string size = FileUpload1.PostedFile.ContentLength.ToString();//文件大小
                string type = FileUpload1.PostedFile.ContentType;//文件类型
                //Path.GetExtension(name);//指定字符串的扩展名  .exe
                string type2 = name.Substring(name.LastIndexOf(".") + 1);//文件扩展名
                //if (!Directory.Exists(pages.Server.MapPath("../"+Pathfile) + "\\"))//判断该文件夹是否存在
                //{
                //    Directory.CreateDirectory(pages.Server.MapPath("../"+Pathfile) + "\\");
                //}
                string ipath = pages.Server.MapPath("../" + Pathfile) + "\\" + name;//-----------文件上传服务器的路径
                string wpath = Pathfile + "/" + name;//返回将在保存在数据库的相对路径的字符串
                string ipath1 = pages.Server.MapPath(Pathfile) + "\\" + name;//-----------文件上传服务器的路径
                if (Convert.ToInt32(size) > filesize)
                {
                    CurrFileSize = "";
                    CurrFileType = "";
                    list.Add(1);
                    list.Add(size);//加载实际大小
                    list.Add(filetype);//类型标识
                    return list;
                }
                else
                {
                    #region *****************************
                    if (types == 0)//只能上传图片
                    {                    if (type2 == "jpg" || type2 == "gif" || type2 == "png")
                        {
                            string FileDateName = DateTime.Now.ToString().Replace(":", "").Replace(" ", "").Replace("-", "");//截取后的时间字符串
                            string FileExtenstion = Path.GetExtension(name);//文件的扩展名
                            name = FileDateName + FileExtenstion;//如果文件类型为图片类型,对文件名进行更改(如:200905152315.jpg)
                            ipath = pages.Server.MapPath("../" + Pathfile + "/" + name);//(要保存的路径)
                            filetype = 0;
                            FileUpload1.SaveAs(ipath);//保存路径上传到服务器
                            CurrFileSize = size;//返回文件的大小
                            CurrFileType = type2;//返回文件的类型
                            list.Add(3);
                            list.Add(Pathfile + "/" + name);
                            list.Add(CurrFileSize);
                            list.Add(CurrFileType);
                            list.Add(filetype);
                            return list;
                        }
                        else
                        {
                            CurrFileSize = "";
                            CurrFileType = "";
                            list.Add(2);//标识符
                            list.Add(type2);
                            list.Add(filetype);
                            return list;
                        }
                    }
                    else if (types == 1)//只能上传视频
                    {
                        if (type2 == "flv")
                        {
                            filetype = 1;
                            FileUpload1.SaveAs(ipath);//保存路径上传到服务器
                            CurrFileSize = size;//返回文件的大小
                            CurrFileType = type2;//返回文件的类型
                            list.Add(3);
                            list.Add(wpath);
                            list.Add(CurrFileSize);
                            list.Add(CurrFileType);
                            list.Add(filetype);
                            return list;
                        }
                        else
                        {
                            CurrFileSize = "";
                            CurrFileType = "";
                            list.Add(2);//标识符
                            list.Add(type2);
                            list.Add(filetype);
                            return list;
                        }
                    }
                    else if (types == 2)//只能上传视频
                    {
                        if (type2 == "txt" || type2 == "doc" || type2 == "docx")
                        {
                            filetype = 2;
                            FileUpload1.SaveAs(ipath1);//保存路径上传到服务器
                            CurrFileSize = size;//返回文件的大小
                            CurrFileType = type2;//返回文件的类型
                            list.Add(3);
                            list.Add(wpath);
                            list.Add(CurrFileSize);
                            list.Add(CurrFileType);
                            list.Add(filetype);
                            return list;
                        }
                        else
                        {
                            CurrFileSize = "";
                            CurrFileType = "";
                            list.Add(2);//标识符
                            list.Add(type2);
                            list.Add(filetype);
                            return list;
                        }
                    }
                    else
                    {
                        CurrFileSize = "";
                        CurrFileType = "";
                        return null;
                    }
                    #endregion
                }
            }
            else
            {
                CurrFileSize = "";
                CurrFileType = "";
                return null;
            }        #region
            //        if (type2 == "jpg" || type2 == "gif" || type2 == "png" || type2 == "doc" || type2 == "rar" || type2 == "txt" || type2 == "xls" || type2 == "wmv" || type2 == "swf")//指定文件上传的格式
            //        {
            //            if (type2 == "jpg" || type2 == "gif" || type2 == "png")
            //            {
            //                string FileDateName = DateTime.Now.ToString().Replace(":", "").Replace(" ", "").Replace("-", "");//截取后的时间字符串
            //                string FileExtenstion = Path.GetExtension(name);//文件的扩展名
            //                name = FileDateName + FileExtenstion;//如果文件类型为图片类型,对文件名进行更改(如:200905152315.jpg)
            //                ipath = pages.Server.MapPath("../"+Pathfile+"/" + name);//(要保存的路径)
            //                filetype = 0;
            //            }
            //            if ( type2 == "wmv" || type2 == "swf")
            //            {
            //                filetype = 1;
            //            }
            //            else
            //            {
            //                filetype = 2;
            //            }
            //            FileUpload1.SaveAs(ipath);//保存路径上传到服务器
            //            CurrFileSize = size;//返回文件的大小
            //            CurrFileType = type2;//返回文件的类型
            //            list.Add(3);
            //            list.Add(wpath);
            //            list.Add(CurrFileSize);
            //            list.Add(CurrFileType);
            //            list.Add(filetype);
            //            return list ;
            //        }
            //        else
            //        {
            //            CurrFileSize = "";
            //            CurrFileType = "";
            //            list.Add(2);//标识符
            //            list.Add(type2);
            //            list.Add(filetype);
            //            return list;
            //        }
            //    }
            //}
            //else
            //{
            //    CurrFileSize = "";
            //    CurrFileType = "";
            //    return null;
            //}
            #endregion
        }
        #endregion
        #region 文件上传并以流的形式存到数据库中
        /// <summary>
        /// 
        /// </summary>
        /// <param name="FileUpload1"></param>
        /// <param name="filesize"></param>
        /// <param name="ArrayByte"></param>
        /// <returns></returns>
        public static ArrayList UploadFile(System.Web.UI.WebControls.FileUpload FileUpload1, int filesize, out byte[] ArrayByte)
        {
            ArrayList list = new ArrayList();
            string CurrenSize = FileUpload1.PostedFile.ContentLength.ToString();
            string CurrenType = FileUpload1.PostedFile.ContentType;
            string type2 = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
            if (FileUpload1.FileName != "")
            {
                if (int.Parse(CurrenSize) > filesize)
                {
                    list.Add(1);
                    list.Add(CurrenSize);
                    ArrayByte = null;
                    return list;
                }
                else
                {
                    if (type2 == "jpg" || type2 == "gif" || type2 == "png" || type2 == "doc" || type2 == "rar" || type2 == "txt" || type2 == "xls")//指定文件上传的格式
                    {
                        ArrayByte = FileUpload1.FileBytes;//获得文件流并写入数组中
                        //或者这样写入流
                        // Stream st= FileUpload1.PostedFile.InputStream;
                        //将流读到数组中`
                        //  int i = st.Read(ArrayByte, 0, st.Length);
                        list.Add(3);
                        list.Add(CurrenSize);
                        list.Add(CurrenType);
                        return list;
                    }
                    else
                    {
                        list.Add(2);//标识符
                        list.Add(type2);
                        ArrayByte = null;
                        return list;
                    }
                }
            }
            else
            {
                ArrayByte = null;
                return null;
            }
        }
      

  2.   

    放在一个类里
    用asp:buttom按钮驱动这个方法
      

  3.   

    我在只要上传.flv文件,就不执行  asp:buttom事件啊,更别说调用类了。
    一点上传就出错:找不到服务器
    奇怪你们不会嘛
    如果上传图片就没问题