编程语言
首页 > 编程语言> > ASP.NET(C#)上传图片(二进制)

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

作者:互联网

原文链接:http://www.cnblogs.com/hakuci/archive/2008/03/23/1118538.html 此方法适合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

转载于:https://www.cnblogs.com/hakuci/archive/2008/03/23/1118538.html

标签:文件,ASP,return,C#,hifile,FileLen,NET,上传,FileData
来源: https://blog.csdn.net/weixin_30849403/article/details/99535811