ASP.NET(C#)上传图片(二进制)

此方法适合ASP.NET(C#)+Oracle
Oracle 表中字段类型为Blob
#region 上传文件(二进制)
        //
        static public Byte[] FileToBinary(FileUpload hifile)
        {
            try
            {
                if (hifile.HasFile)
                {
                    if (IsAllowedExtension(hifile))
                    {
                        //取得上传文件的大小
                        int FileLen = hifile.PostedFile.ContentLength;

                        Byte[] FileData = new Byte[FileLen];

                        //创建访问客户端上传文件的对象
                        HttpPostedFile hp = hifile.PostedFile;

                        //创建数据流对象
                        Stream sr = hp.InputStream;

                        //将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置
                        sr.Read(FileData, 0, FileLen);

                        //string id = "0";
                        //BLL.Picture.AddPictures(id,"name",Convert.ToString(FileData),"url","说明");

                        return FileData;//返回二进制文件流 只要把它存入数据库即可

                    }
                    else
                    {
                        //Response.Write("<script languge = ‘javascript‘ type = ‘text/javascript‘>alert(‘上传文件格式不对,只允许上传jpg和gif格式的文件‘);</script>");
                        return null;// "上传文件格式不对,只允许上传jpg和gif格式的文件";
                    }
                }
                else
                {
                    return null;// "请选择上传文件";
                    //Response.Write("<script language = ‘javascript‘ type = ‘text/javascript‘>alert(‘请选择上传文件‘);</script>");
                }
            }
            catch
            {
                return null;
            }
        }
        #endregion

posted on 2008-03-23 16:19  黄兆张's Blog  阅读(2931)  评论(1编辑  收藏  举报

导航