📄 filestream.ashx
字号:
<%@ WebHandler Language="C#" Class="FileStream" %>
using System;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Reponsible for flushing out the file from the database to the user.
/// </summary>
public class FileStream : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
// get the file id
Guid fileId = new Guid(context.Request.QueryString["FileId"]);
using (SqlCommand command = new SqlCommand())
{
// get the file from database
command.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
command.CommandText = "select * from [Files] where FileId = @FileId";
command.Parameters.Add("@FileId", SqlDbType.UniqueIdentifier).Value = fileId;
command.Connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
// flush out the binary data to the user
context.Response.Clear();
context.Response.ContentType = (string) reader["FileType"];
context.Response.AddHeader("Content-Disposition", String.Format("inline;filename={0};", reader["FileName"].ToString()));
context.Response.AddHeader("Content-Length", reader["FileSize"].ToString());
context.Response.BinaryWrite((byte[]) reader["FileContent"]);
context.Response.End();
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -