ITeye博客

第四章:使用Rich控件

cpsing · 2008-10-20 23:38 · Markdown

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

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

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

}


<o:p>

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

配置文件, 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();
    }


</o:p>

读取数据库大二进制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);
                               
            }
        }

<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>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。