62,046
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
}
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();
}
<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>