string filePath = "F:\\娱乐\\;//设置保存路径
byte[] file = null; SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=server;database=archives; user id=sa;password=123";
conn.Open();
string sql = "select file_content from multimedia "; SqlCommand cm = new SqlCommand();
cm.CommandText = sql;
cm.CommandType = CommandType.Text;
cm.Connection = conn;
SqlDataReader dr = cm.ExecuteReader(); while (dr.Read())
{
if (dr["file_content"].ToString() != "")
{ file = (byte[])dr["file_content"]; }
}
自己写的代码可以下载,但不提示保存对话框,新人敬请高人,小分相送,谢谢
byte[] file = null; SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=server;database=archives; user id=sa;password=123";
conn.Open();
string sql = "select file_content from multimedia "; SqlCommand cm = new SqlCommand();
cm.CommandText = sql;
cm.CommandType = CommandType.Text;
cm.Connection = conn;
SqlDataReader dr = cm.ExecuteReader(); while (dr.Read())
{
if (dr["file_content"].ToString() != "")
{ file = (byte[])dr["file_content"]; }
}
自己写的代码可以下载,但不提示保存对话框,新人敬请高人,小分相送,谢谢
public class DownloadHandler : IHttpHandler
{
/// <summary>
///功能说明:文件下载类--不管是什么格式的文件,都能够弹出打开/保存窗口,
///包括使用下载工具下载
///继承于IHttpHandler接口,可以用来自定义HTTP 处理程序同步处理HTTP的请求
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
HttpResponse Response = context.Response;
HttpRequest Request = context.Request; System.IO.Stream iStream = null; byte[] buffer = new Byte[10240]; int length; long dataToRead; try
{
string filename = FileHelper.Decrypt(Request["fn"]); //通过解密得到文件名 string filepath = HttpContext.Current.Server.MapPath("~/") + filename; //待下载的文件路径 iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read);
Response.Clear(); dataToRead = iStream.Length; long p = 0;
if (Request.Headers["Range"] != null)
{
Response.StatusCode = 206;
p = long.Parse(Request.Headers["Range"].Replace("bytes=", "").Replace("-", ""));
}
if (p != 0)
{
Response.AddHeader("Content-Range", "bytes " + p.ToString() + "-" + ((long)(dataToRead - 1)).ToString() + "/" + dataToRead.ToString());
}
Response.AddHeader("Content-Length", ((long)(dataToRead - p)).ToString());
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.GetEncoding(65001).GetBytes(Path.GetFileName(filename)))); iStream.Position = p;
dataToRead = dataToRead - p; while (dataToRead > 0)
{
if (Response.IsClientConnected)
{
length = iStream.Read(buffer, 0, 10240); Response.OutputStream.Write(buffer, 0, length);
Response.Flush(); buffer = new Byte[10240];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -1;
}
}
}
catch (Exception ex)
{
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
iStream.Close();
}
Response.End();
}
} public bool IsReusable
{
get { return true; }
}
} public class FileHelper
{
/// <summary>
/// 利用Base64码对文件名进行加密处理。
/// </summary>
/// <param name="filename">文件名</param>
/// <returns></returns>
public static string Encrypt(string filename)
{
byte[] buffer = HttpContext.Current.Request.ContentEncoding.GetBytes(filename);
return HttpUtility.UrlEncode(Convert.ToBase64String(buffer));
} /// <summary>
/// 利用Base64码对文件名进行解密处理。
/// </summary>
/// <param name="encryptfilename">文件名</param>
/// <returns></returns>
public static string Decrypt(string encryptfilename)
{
byte[] buffer = Convert.FromBase64String(encryptfilename);
return HttpContext.Current.Request.ContentEncoding.GetString(buffer);
} }
web.config设置: <httpHandlers>
<add verb="*" path="download.aspx" type="命名空间.DownloadHandler"/>
</httpHandlers>//前台页面: protected void Button1_Click(object sender, EventArgs e)
{
string url = FileHelper.Encrypt("下载文件");
Response.Redirect("~/download.aspx?fn=" + url);//解密下载
}