public partial class Download : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { try { if (!String.IsNullOrEmpty(Request["mid"])) { MediaItem mi = Sitecore.Context.Database.GetItem(new ID(Request["mid"])); Stream fileStream = mi.GetMediaStream(); byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, (int)fileStream.Length); fileStream.Close(); Response.ContentType = mi.MimeType; Response.AddHeader("content-disposition", "attachment; filename=" + mi.DisplayName + "." + mi.Extension); Response.BinaryWrite(buffer); Response.Flush(); Response.End(); } else { if (String.IsNullOrEmpty(Request["fileUrl"]) || String.IsNullOrEmpty(Request["fileType"])) { Page.ClientScript.RegisterStartupScript(Page.GetType(), "script", "<script type=‘text/javascript‘>alert(‘下载失败‘);window.opener=null;window.open(‘‘,‘_self‘); window.close();</script>"); } else { string url = "http://" + System.Web.HttpContext.Current.Request.Url.Host.ToString() + ":" + System.Web.HttpContext.Current.Request.Url.Port.ToString() + Request["fileUrl"]; WebClient my = new WebClient(); byte[] mybyte; mybyte = my.DownloadData(url); MemoryStream ms = new MemoryStream(mybyte); //下面直接输出 Response.ClearContent(); Response.ContentType = "application/x-msdownload"; string filename = "attachment; filename=" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + Request["fileType"]; Response.AddHeader("Content-Disposition", filename); Response.BinaryWrite(mybyte); Response.Flush(); Response.End(); } } } catch { Page.ClientScript.RegisterStartupScript(Page.GetType(), "script", "<script type=‘text/javascript‘>alert(‘下载失败‘);window.opener=null;window.open(‘‘,‘_self‘); window.close();</script>"); } } }
时间: 2024-11-05 06:04:31