第四章:使用Rich控件

阅读更多

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

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

Code
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;
        }
    }

}


*** 上传大文件

配置文件, httpRuntime maxRequestLength 和 httpRuntime requestLenghtDiskThreshold.

Code
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();
    }

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


Code
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);
                               
            }
        }

***

使用Memu控件和Muliview可以实现tab标签调用效果。

重要的是使用css

   .tab

        {

            border:solid 1px black;

            background-color:#eeeeee;

            padding:2px 10px;

        }

        .selectedTab

        {

            background-color:white;

            border-bottom:solid 1px white;

        }


***

使用Memu控件和Muliview可以实现tab标签调用效果。

重要的是使用css

   .tab

        {

            border:solid 1px black;

            background-color:#eeeeee;

            padding:2px 10px;

        }

        .selectedTab

        {

            background-color:white;

            border-bottom:solid 1px white;

        }

***

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

NextView ----commandName

PreView    ----commandName

SwitchViewByID  ------CommanArgument

SwichViewByIndex ------CommanArgument

***

Wizard 控件用于向导式的表单跳转。

你可能感兴趣的:(CSS,编程,Web,Security)