首页 > 代码库 > Sitcore Download MediaLibrary Source
Sitcore Download MediaLibrary Source
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>"); } } }
Sitcore Download MediaLibrary Source
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。