第四章:使用Rich控件

.ashx 文件用于写web handler的。其实就是带HTML和C#的混合文件。当然你完全可以用.aspx 的文件后缀。使用.ashx 可以让你专注于编程而不用管相关的WEB技术。<o:p></o:p>

可以用于不需要表现页面的处理程序。

ContractedBlock.gifExpandedBlockStart.gifCode
public class FileHandler : IHttpHandler {

    
const string conString = @"Server=.\SQLExpress;Integrated Security=True;
        AttachDbFileName=|DataDirectory|FilesDB.mdf;User Instance=True
";
    
    
public void ProcessRequest (HttpContext context) {
        context.Response.ContentType 
= "application/msword";
        
        SqlConnection con 
= new SqlConnection(conString);
        SqlCommand cmd 
= new SqlCommand("SELECT FileBytes FROM Files WHERE Id=@Id", con);
        cmd.Parameters.AddWithValue(
"@Id", context.Request["Id"]);
        
using (con)
        {
            con.Open();
            
byte[] file = (byte[])cmd.ExecuteScalar();
            context.Response.BinaryWrite(file);
        }
    }
 
    
public bool IsReusable {
        
get {
            
return false;
        }
    }

}


<o:p>

*** 上传大文件<o:p></o:p>

配置文件, httpRuntime maxRequestLength 和 httpRuntime requestLenghtDiskThreshold.

ContractedBlock.gifExpandedBlockStart.gifCode
void AddFile(string fileName, Stream upload)
    {
        SqlConnection con 
= new SqlConnection(conString);

        SqlCommand cmd 
= new SqlCommand("INSERT Files (FileName) Values (@FileName);" +
          
"SELECT @Identity = SCOPE_IDENTITY()", con);

        cmd.Parameters.AddWithValue(
"@FileName", fileName);
        SqlParameter idParm 
= cmd.Parameters.Add("@Identity", SqlDbType.Int);
        idParm.Direction 
= ParameterDirection.Output;

        
using (con)
        {
            con.Open();
            cmd.ExecuteNonQuery();
            
int newFileId = (int)idParm.Value;
            StoreFile(newFileId, upload, con);
        }
    }
    
    
void StoreFile(int fileId, Stream upload, SqlConnection connection)
    {
        
int bufferLen = 8040;
        BinaryReader br 
= new BinaryReader(upload);
        
byte[] chunk = br.ReadBytes(bufferLen);

        SqlCommand cmd 
= new SqlCommand("UPDATE Files SET FileBytes=@Buffer WHERE Id=@FileId", connection);
        cmd.Parameters.AddWithValue(
"@FileId", fileId);
        cmd.Parameters.Add(
"@Buffer", SqlDbType.VarBinary, bufferLen).Value = chunk;
        cmd.ExecuteNonQuery();
                
        
        SqlCommand cmdAppend 
= new SqlCommand("UPDATE Files SET FileBytes .WRITE(@Buffer, NULL, 0) WHERE Id=@FileId", connection);
        cmdAppend.Parameters.AddWithValue(
"@FileId", fileId);
        cmdAppend.Parameters.Add(
"@Buffer", SqlDbType.VarBinary, bufferLen);
        chunk 
= br.ReadBytes(bufferLen);
        
        
while (chunk.Length > 0)
        {
            cmdAppend.Parameters[
"@Buffer"].Value = chunk;
            cmdAppend.ExecuteNonQuery();
            chunk 
= br.ReadBytes(bufferLen);
        }

        br.Close();
    }

</o:p>

读取数据库大二进制byte[]


ContractedBlock.gifExpandedBlockStart.gifCode
context.Response.Buffer = false;
using (con)
        {
            con.Open();
            SqlDataReader reader 
= cmd.ExecuteReader(CommandBehavior.SequentialAccess);
            
if (reader.Read())
            {
                
int bufferSize = 8040;
                
byte[] chunk = new byte[bufferSize];
                
long retCount;
                
long startIndex = 0;

                retCount 
= reader.GetBytes(0, startIndex, chunk, 0, bufferSize);

                
                
while (retCount == bufferSize)
                {
                    context.Response.BinaryWrite(chunk);

                    startIndex 
+= bufferSize;
                    retCount 
= reader.GetBytes(0, startIndex, chunk, 0, bufferSize);
                }

                
byte[] actualChunk = new Byte[retCount - 1];
                Buffer.BlockCopy(chunk, 
0, actualChunk, 0, (int)retCount - 1);
                context.Response.BinaryWrite(actualChunk);
                               
            }
        }

<o:p></o:p>

***<o:p></o:p>

使用Memu控件和Muliview可以实现tab标签调用效果。<o:p></o:p>

重要的是使用css<o:p></o:p>

   .tab<o:p></o:p>

        {<o:p></o:p>

            border:solid 1px black;<o:p></o:p>

            background-color:#eeeeee;<o:p></o:p>

            padding:2px 10px;<o:p></o:p>

        }<o:p></o:p>

        .selectedTab<o:p></o:p>

        {<o:p></o:p>

            background-color:white;<o:p></o:p>

            border-bottom:solid 1px white;<o:p></o:p>

        }<o:p></o:p>

<o:p></o:p>

<o:p></o:p>

<o:p> </o:p>

<o:p></o:p>

***<o:p></o:p>

使用Memu控件和Muliview可以实现tab标签调用效果。<o:p></o:p>

重要的是使用css<o:p></o:p>

   .tab<o:p></o:p>

        {<o:p></o:p>

            border:solid 1px black;<o:p></o:p>

            background-color:#eeeeee;<o:p></o:p>

            padding:2px 10px;<o:p></o:p>

        }<o:p></o:p>

        .selectedTab<o:p></o:p>

        {<o:p></o:p>

            background-color:white;<o:p></o:p>

            border-bottom:solid 1px white;<o:p></o:p>

        }<o:p></o:p>

***<o:p></o:p>

Mutiview可用于向导式的表单,在表单里的按钮识别以下命令:<o:p></o:p>

NextView ----commandName<o:p></o:p>

PreView    ----commandName<o:p></o:p>

SwitchViewByID  ------CommanArgument<o:p></o:p>

SwichViewByIndex ------CommanArgument<o:p></o:p>

<o:p></o:p>

***<o:p></o:p>

Wizard 控件用于向导式的表单跳转。<o:p></o:p>

<o:p></o:p>

<o:p></o:p>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值