Handler问题求教

倒大霉的上帝 2012-10-29 08:56:59
现在有这样的一个需求:

客户端会请求下载一个文件,这个文件在服务器上存在,但是在下载之前,会根据客户端传过来的参数,对文件进行加密处理,然后供下载。每个客户端的请求的参数值是不一样的。

我想知道这个需求是否可以通过handler来处理?有没简单的例子或资料?谢谢各位大牛。
...全文
185 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
倒大霉的上帝 2012-10-30
  • 打赏
  • 举报
回复
谢谢大家 问题解决了。马上结贴
倒大霉的上帝 2012-10-30
  • 打赏
  • 举报
回复
谢谢楼上的回答,我想再问下。按照上面的做法,客户端应该怎么接收文件呢?
XBodhi. 2012-10-29
  • 打赏
  • 举报
回复
你说的是 HTTP 协议包 还是什么, Handler 还是 Header
倒大霉的上帝 2012-10-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

把传递过来的参数做加密种子,然后生成文件,然后输出到客户端
[/Quote]

谢谢 主要是想了解handler的原理及过程。加密过程没什么问题。
倒大霉的上帝 2012-10-29
  • 打赏
  • 举报
回复
解决了我一会再追加100分。见者有份
lshfong 2012-10-29
  • 打赏
  • 举报
回复
把传递过来的参数做加密种子,然后生成文件,然后输出到客户端
倒大霉的上帝 2012-10-29
  • 打赏
  • 举报
回复
非常感谢大家,我先了解下去。因为对原理和过程还不够了解。
白云任去留 2012-10-29
  • 打赏
  • 举报
回复
类似这个

public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["file"] != null)
{
string path = context.Server.MapPath("/uploadfile/" + context.Request.QueryString["file"]);
FileInfo fi = new FileInfo(path);
if (fi.Exists)
{
context.Response.Clear();
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + context.Server.UrlEncode(fi.Name));
context.Response.AddHeader("Content-Length", fi.Length.ToString());
context.Response.ContentType = "application/octet-stream";
context.Response.Filter.Close();
context.Response.WriteFile(fi.FullName);
context.Response.End();
}
else
{
context.Response.Status = "404 File Not Found";
context.Response.StatusCode = 404;
context.Response.StatusDescription = "File Not Found";
context.Response.Write("File Not Found");
context.Response.End();
}
}
}
快溜 2012-10-29
  • 打赏
  • 举报
回复
重载几个不同的加密方法。根据参数类型调用。
宝_爸 2012-10-29
  • 打赏
  • 举报
回复
我目前工程里的下载页面:


protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;

//取得参数, get和post都可以。
//我这里是Post
string recipeString = Request.Form["recipe"];
if (recipeString == null)
{
lbError.Text = GetLocalResourceObject("ErrorMessageMissingParameter").ToString();
return;
}

string pageId = Request.Form["pageid"];


byte[] content = ....; //取得文件内容

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
Response.AddHeader("Content-Length",content.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(content);
Response.Flush();
Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();

}


发起下载请求页面的html代码:

<div id="divDownload">
<form id="formDownload" action="Download.aspx" method="post" target="_blank">
<input id="hdRecipe" type="hidden" name="recipe" value=""/>
<input id="hdPageId" type="hidden" name="pageid" value=""/>
<br />

</form>

wuyq11 2012-10-29
  • 打赏
  • 举报
回复
FileInfo Fi = new FileInfo("");
if (Fi.Exists)
{
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(dt.Rows[0]["Name"].ToString(), System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
wuyq11 2012-10-29
  • 打赏
  • 举报
回复
在download.aspx根据参数值,读取文件流,再输出文件
宝_爸 2012-10-29
  • 打赏
  • 举报
回复
handler或者aspx都可以。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧