上传图片附件到数据库(Oracle)实例

12 篇文章 0 订阅
7 篇文章 0 订阅

上传图片附件到数据库(Oracle)实例

一、类

    /// <summary>
        
/// 功能:图片附件的上传
        
/// </summary>
        
/// <returns></returns>

         public   int  FileAttachmentAdd()
        
{
            
int intAttachmentID;

            intAttachmentID 
= 0;

            OracleConnection oraConn;
            OracleCommand oraCmd;
            OracleParameter oraParm;
            
//OracleTransaction oraTrans;
            OracleLob blobFileContent;

            
string strQueryString;

            strQueryString 
= "DECLARE temp BLOB; BEGIN DBMS_LOB.CREATETEMPORARY(temp,false,0); :blobFileContent := temp; END;";

            oraConn 
= new OracleConnection();
            oraConn.ConnectionString 
= DBAccess.DBUtil.GetConnectionString();
            oraConn.Open();

            
//oraTrans = oraConn.BeginTransaction();

            oraCmd 
= new OracleCommand();
            oraCmd.Connection 
= oraConn;
            oraCmd.CommandType 
= CommandType.Text;
            oraCmd.CommandText 
= strQueryString;

            oraParm 
= new OracleParameter();
            oraParm.ParameterName 
= "blobFileContent";
            oraParm.Direction 
= ParameterDirection.Output;
            oraParm.OracleType 
= OracleType.Blob;
            oraCmd.Parameters.Add(oraParm);
            
//oraCmd.Transaction = oraTrans;

            oraCmd.ExecuteNonQuery();

            blobFileContent 
= (OracleLob)oraCmd.Parameters["blobFileContent"].Value;

            oraCmd 
= null;

            blobFileContent.BeginBatch(OracleLobOpenMode.ReadWrite);
            blobFileContent.Write(byteFileContent,
0,intFileLength);
            blobFileContent.EndBatch();

            oraCmd 
= new OracleCommand();
            oraCmd.Connection 
= oraConn;
            oraCmd.CommandType 
= CommandType.StoredProcedure;
            oraCmd.CommandText 
= "pkg_TaskItem.up_FileAttachmentAdd";
            
//oraCmd.Transaction = oraTrans;
            
//附件ID
            oraParm = new OracleParameter();
            oraParm.OracleType 
= OracleType.Number;
            oraParm.Direction 
= ParameterDirection.Output;
            oraParm.ParameterName 
= "AttachmentID";
            oraCmd.Parameters.Add(oraParm);
            oraParm 
= null;
            
//文件类型的分类
            oraParm = new OracleParameter();
            oraParm.OracleType 
= OracleType.VarChar;
            oraParm.Direction 
= ParameterDirection.Input;
            oraParm.ParameterName 
= "FileCategory";
            oraParm.Size 
= 50;
            oraParm.Value 
= strFileCategory;
            oraCmd.Parameters.Add(oraParm);
            oraParm 
= null;
            
//文件名称
            oraParm = new OracleParameter();
            oraParm.OracleType 
= OracleType.VarChar;
            oraParm.Direction 
= ParameterDirection.Input;
            oraParm.ParameterName 
= "FileName";
            oraParm.Size 
= 50;
            oraParm.Value 
= strFileName;
            oraCmd.Parameters.Add(oraParm);
            oraParm 
= null;
            
//文件的后缀名称(jpeg)
            oraParm = new OracleParameter();
            oraParm.OracleType 
= OracleType.VarChar;
            oraParm.Direction 
= ParameterDirection.Input;
            oraParm.ParameterName 
= "FileType";
            oraParm.Size 
= 50;
            oraParm.Value 
= strFileType;
            oraCmd.Parameters.Add(oraParm);
            oraParm 
= null;
            
//文件的长度
            oraParm = new OracleParameter();
            oraParm.OracleType 
= OracleType.Number;
            oraParm.Direction 
= ParameterDirection.Input;
            oraParm.ParameterName 
= "FileLength";
            oraParm.Value 
= intFileLength;
            oraCmd.Parameters.Add(oraParm);
            oraParm 
= null;
            
//文件的大小
            oraParm = new OracleParameter();
            oraParm.OracleType 
= OracleType.Blob;
            oraParm.Direction 
= ParameterDirection.Input;
            oraParm.ParameterName 
= "FileContent";
            oraParm.Value 
= blobFileContent;
            oraCmd.Parameters.Add(oraParm);
            oraParm 
= null;

            oraCmd.ExecuteNonQuery();
            
//oraTrans.Commit();
                
            intAttachmentID 
= Convert.ToInt32( oraCmd.Parameters["AttachmentID"].Value.ToString());

            oraConn.Close();            
            oraCmd 
= null;
            oraConn 
= null;

            
return intAttachmentID;
        }

二、页面

//页面本地图片显示及删除
   < script language = " javascript "  id = " clientEventHandlersJS " >
        
<!--
            
// 更改图片
             function  ImgFile1_onchange()  {
                
var Img = document.getElementById("Img1");
                
var ImgFile = document.getElementById("ImgFile1");
                
var hiddenInput = document.getElementById("txthidden1");
                Img.src 
= ImgFile.value;
                hiddenInput.value 
= "1";
            }

            
function  ImgFile2_onchange()  {
                
var Img = document.getElementById("Img2");
                
var ImgFile = document.getElementById("ImgFile2");
                
var hiddenInput = document.getElementById("txthidden2");
                Img.src 
= ImgFile.value;
                hiddenInput.value 
= "1";
            }


            
// 清除图片及路径
             function  ClearImg1()
            
{
                
var Img = document.getElementById("Img1");
                Img.src 
= "../../Images/Pole/NoPoleView.gif";
                
var hiddenInput = document.getElementById("txthidden1");
                hiddenInput.value 
= "0";
                document.all.ImgFile1.outerHTML
+='';
                
return false;
            }


            
function  ClearImg2()
            
{
                
var Img = document.getElementById("Img2");
                
var hiddenInput = document.getElementById("txthidden2");
                Img.src 
= "../../Images/Pole/NoPoleView.gif";
                hiddenInput.value 
= "0";
                document.all.ImgFile2.outerHTML
+='';
                
return false;
            }

        
// -->
         </ script >
/// <summary>
        
/// 功能:上传图片文件并得到文件ID
        
/// </summary>
        
/// <param name="UploadFile"></param>
        
/// <param name="FileName"></param>
        
/// <returns></returns>

         private   int  UpLoadImage(HtmlInputFile UploadFile, string  FileName)
        
{
            
int intFileAttachmentID = 0;
            Archive.Components.ErrorImage objImage 
= new PM.Archive.Components.ErrorImage();

            
byte[] byteFile;
            HttpPostedFile httpPostFile;    
//客户端上载文件对象
            httpPostFile = UploadFile.PostedFile;
//            string strTemp = "";
//            string strFileName;                //文件名             
//            strTemp = httpPostFile.FileName.ToString().Trim();
//            strFileName = GetRealFileName(strTemp);
//            if(strFileName.Length > 25)
//            {
//                throw new Exception("文件名称应在25字以内!");
//            }
//            else
//            {
                System.IO.Stream stream = httpPostFile.InputStream;
                byteFile 
= FileContentsGet(stream,httpPostFile);
                
int intFileLength = httpPostFile.ContentLength;                
                
string strTypeName = UploadFile.PostedFile.ContentType;
                
if(strTypeName.ToLower().IndexOf("image"< 0)
                
{
                    
throw new Exception("上传文件必须是图片文件!");
                }

                
                
decimal decFileSize;
                decFileSize 
= System.Convert.ToDecimal(intFileLength / 1024.0 );
                
if(decFileSize > 4096)
                
{
                    
throw new Exception("上传文件请小于4M!");
                }

                
else
                
{
                    objImage.FileCategory 
= "ErrorImage";
                    objImage.FileName 
= FileName;
                    objImage.FileType 
= strTypeName;
                    objImage.FileLength 
= intFileLength;
                    objImage.FileContent 
= byteFile;

                    intFileAttachmentID 
= objImage.FileAttachmentAdd();
                }

//            }

            
return intFileAttachmentID;
        }


        
/// <summary>
        
/// 功能:将上传附件内容读入字节数组中方法
        
/// </summary>
        
/// <param name="stream">Stream流</param>
        
/// <param name="fileAttachment">客户端上载文件对象</param>
        
/// <returns>附件的二进制字节信息</returns>

         public   byte  [] FileContentsGet(System.IO.Stream stream,HttpPostedFile httpPostFile)
        
{
            
int intImageLength = httpPostFile.ContentLength;

            
if (intImageLength == 0)
            
{
                intImageLength 
= 1;
            }

            
            
byte [] byteImageContent = new byte[intImageLength];

            stream.Read(byteImageContent, 
0, (int)stream.Length);

            
return byteImageContent;
        }

三、存储过程

-- ******************************************************************
     -- 功能:附件的新增
     -- ******************************************************************
     PROCEDURE  up_FileAttachmentAdd
    (
        AttachmentID OUT 
NUMBER ,         -- 输出的附件ID
        FileCategory     VARCHAR2 ,          -- 上传图片类型  如"用电村低压地理接线图"
        FileName         VARCHAR2 ,        -- 上传附件文件名称
        FileType         VARCHAR2 ,        -- 上传文件类型如jpg
        FileLength         NUMBER ,          -- 上传附件大小
        FileContent    BLOB            --  上传附件内容
    )
    
IS
    
BEGIN
        
-- 取库存记录ID
         SELECT   seq_FileAttachment.NEXTVAL   INTO   AttachmentID  FROM  DUAL;
        
-- 插入数据
         INSERT   INTO  t_FileAttachment
        (
            f_AttachmentID,
            f_AttachmentCategory,
            f_FileContent,
            f_FileType,
            f_FileName,
            f_FileLength
        )
        
VALUES
        (
            AttachmentID,
            FileCategory,
            FileContent,
            FileType,
            FileName,
            FileLength
        );

     
END  up_FileAttachmentAdd;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值