上传/读取文件

  =========数据库脚本==========  
drop table tb_SendFile

 
CREATE TABLE tb_SendFile
 (
    i_ID 
int identity(1,1CONSTRAINT PK_SendFile_id PRIMARY KEY,
    ch_ImageID 
nvarchar(50),
    img_ImageData 
image,
    ch_ImageType 
nvarchar(50),
    FileLength 
int
 )
 
 
SELECT * FROM tb_SendFile
 
 
INSERT INTO tb_SendFile
 (
    ch_ImageID,
    img_ImageData
    
 )
  
VALUES(1,'abcdee')
  
 
DROP PROCEDURE sp_SendFile_Add
 
CREATE PROCEDURE sp_SendFile_Add
 
@ch_ImageID nvarchar(50),
 
@img_ImageData image,
 
@FileLength int,
 
@ch_ImageType nvarchar(50),
 
@id int OUTPUT
 
AS
  
INSERT INTO tb_SendFile
 (
    ch_ImageID,
    img_ImageData,
    ch_ImageType,
    FileLength
 )
  
VALUES(@ch_ImageID,@img_ImageData,@ch_ImageType,@FileLength)
  
SELECT 
  
@id=@@Identity
  
GO
  
  
DROP PROCEDURE sp_SendFile_Read
  
  
CREATE PROCEDURE sp_SendFile_Read
 
@ch_ImageID nvarchar(50),
 
@img_ImageData image OUTPUT,
 
@ch_ImageType nvarchar(50) OUTPUT,
 
@FileLength int OUTPUT
  
AS
  
SELECT
 
-- ch_ImageID,
    @img_ImageData=img_ImageData,
    
@FileLength=FileLength,
    
@ch_ImageType=ch_ImageType
    
FROM tb_SendFile
    
WHERE(ch_ImageID=@ch_ImageID)
    
  

====写入数据库=========
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendFile.aspx.cs" Inherits="AssetsManager.Test.SendFile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<br />
        
<br />
        
<asp:Label ID="Label1" runat="server" Text="图号"></asp:Label>
        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        
<br />
        
<asp:Label ID="Label2" runat="server" Text="要上传的文件"></asp:Label>
        
<input id="File1" type="file" runat="server"/><br />
        
<asp:Button ID="BtnOK" runat="server" Text="OK" Width="98px" OnClick="BtnOK_Click" /></div>
    
</form>
</body>
</html>
=================================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace AssetsManager.Test
{
    
public partial class SendFile : System.Web.UI.Page
    
{
        SqlConnection objConn 
= null;
        SqlCommand objComm 
= null;
        
protected void Page_Load(object sender, EventArgs e)
        
{
        }

        
protected void BtnOK_Click(object sender, EventArgs e)
        
{
            
string strConn = "server=.;database=AssetsManagerDB; user id=sa;password=WangJun2005";
            objConn 
= new SqlConnection(strConn);
            
            
//-----------------
            HttpPostedFile Up_File = File1.PostedFile;
            Int32 FileLength 
= 0;
            FileLength 
= Up_File.ContentLength;
            Byte[] FileByteArray
=new Byte[FileLength];
            Stream objStream 
= Up_File.InputStream;
            
string typeValue = Up_File.ContentType;
            objStream.Read(FileByteArray, 
0, FileLength);
            
//---------------------------
            objComm = new SqlCommand("sp_SendFile_Add", objConn);
            objComm.CommandType 
= CommandType.StoredProcedure;
            SqlParameter ParameterID 
= new SqlParameter("@ch_ImageID", SqlDbType.NVarChar, 50);
            ParameterID.Value 
= TextBox1.Text.Trim().ToString();
            objComm.Parameters.Add(ParameterID);

            SqlParameter ParameterSendData 
= new SqlParameter("@img_ImageData", SqlDbType.Binary, FileLength);
            ParameterSendData.Value 
= FileByteArray;
            objComm.Parameters.Add(ParameterSendData);
            
            SqlParameter ParameterFileLength 
= new SqlParameter("@FileLength", SqlDbType.Int, 4);
            ParameterFileLength.Value 
= FileLength;
            objComm.Parameters.Add(ParameterFileLength);
            
            SqlParameter ParameterImageType 
= new SqlParameter("@ch_ImageType", SqlDbType.NVarChar, 50);
            ParameterImageType.Value 
= typeValue;
            objComm.Parameters.Add(ParameterImageType);

            SqlParameter ParameterIdReturn 
= new SqlParameter("@id", SqlDbType.Int, 4);
            ParameterIdReturn.Direction 
= ParameterDirection.Output;
            objComm.Parameters.Add(ParameterIdReturn);

            
int checkValue = 0;
            
try
            
{
                objConn.Open();
                objComm.ExecuteNonQuery();

                checkValue 
= (int)ParameterIdReturn.Value;
                
if (objConn != null)
                    objConn.Close();
            }

            
catch (SqlException ex)
            
{
                
return;
            }

            
if (checkValue.ToString() != "")
            
{
                Response.Write(
"<script>alert('数据添加成功!');</script>");
            }

        }

    }

}

===========读出=================
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReadFile.aspx.cs" Inherits="AssetsManager.Test.ReadFile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        
<asp:Button ID="Button1" runat="server" Text="读出上传的文件" Width="152px" OnClick="Button1_Click" /></div>
    
</form>
</body>
</html>
=================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
namespace AssetsManager.Test
{
    
public partial class ReadFile : System.Web.UI.Page
    
{
        SqlConnection objConn 
= null;
        SqlCommand objComm 
= null;

        
protected void Page_Load(object sender, EventArgs e)
        
{
        }

        
protected void Button1_Click(object sender, EventArgs e)
        
{
            String strConn 
= "server=.;database=AssetsManagerDB;user id=sa;password=WangJun2005";
            objConn 
= new SqlConnection(strConn);
            objComm 
= new SqlCommand("sp_SendFile_Read", objConn);
            objComm.CommandType 
= CommandType.StoredProcedure;
            SqlParameter ParameterID 
= new SqlParameter("@ch_ImageID", SqlDbType.NVarChar, 50);
            ParameterID.Value 
= TextBox1.Text.Trim().ToString();
            objComm.Parameters.Add(ParameterID);

            SqlParameter ParameterImageData 
= new SqlParameter("@img_ImageData", SqlDbType.Binary, 718863);
            ParameterImageData.Direction 
= ParameterDirection.Output;
            objComm.Parameters.Add(ParameterImageData);

            SqlParameter ParameterImageType 
= new SqlParameter("@ch_ImageType", SqlDbType.NVarChar, 50);
            ParameterImageType.Direction 
= ParameterDirection.Output;
            objComm.Parameters.Add(ParameterImageType);

            SqlParameter ParameterFileLength 
= new SqlParameter("@FileLength", SqlDbType.Int, 4);
            ParameterFileLength.Direction 
= ParameterDirection.Output;
            objComm.Parameters.Add(ParameterFileLength);
            
try
            
{
                objConn.Open();
                objComm.ExecuteNonQuery();
                Byte[] objArray 
= (Byte[])ParameterImageData.Value;
                
int iFileLength = (int)ParameterFileLength.Value;
                Response.ContentType 
= (string)ParameterImageType.Value;
                Response.OutputStream.Write(objArray, 
0, iFileLength);
                Response.End();
            }

            
catch (SqlException ex)
            
{
                
return;
            }

            
finally
            
{
                
if (objConn != null)
                    objConn.Close();
            }

        }

    }

}


posted @ 2007-11-21 15:35  wj-conquer  阅读(228)  评论(0编辑  收藏  举报