Exemplo n.º 1
0
        /// <summary>
        ///     Enables processing of the result of an action method by a custom type that inherits from the
        ///     <see cref="T:System.Web.Mvc.ActionResult" /> class.
        /// </summary>
        /// <param name="context">
        ///     The context in which the result is executed. The context information includes the controller,
        ///     HTTP content, request context, and route data.
        /// </param>
        public override void ExecuteResult(ControllerContext context)
        {
            var filePath = String.IsNullOrEmpty(this.VirtualBasePath)
                ? context.HttpContext.Request.MapPath(this.VirtualPath)
                : context.HttpContext.Request.MapPath(this.VirtualPath, this.VirtualBasePath, false);

            if (!String.IsNullOrEmpty(this.FileDownloadName))
            {
                context.HttpContext.Response.AddHeader(
                    "content-disposition",
                    "attachment; filename=" + this.FileDownloadName);
            }

            if (!String.IsNullOrEmpty(this.ContentType))
            {
                context.HttpContext.Response.ContentType = this.ContentType;
            }
            else
            {
                var extIndex = filePath.LastIndexOf('.');
                if (extIndex > -1)
                {
                    var fileExt = filePath.Substring(extIndex);
                    if (!string.IsNullOrEmpty(fileExt) && ExtensionMapper.Contains(fileExt))
                    {
                        context.HttpContext.Response.ContentType = ExtensionMapper.GetContentType(fileExt);
                    }
                }
            }

            try
            {
                context.HttpContext.Response.TransmitFile(filePath);
                context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.HttpContext.Response.Cache.SetMaxAge(TimeSpan.FromHours(1));
            }
            catch (FileNotFoundException)
            {
                if (this.FallbackPaths.ContainsKey(context.HttpContext.Response.ContentType))
                {
                    filePath =
                        context.HttpContext.Request.MapPath(
                            this.FallbackPaths[context.HttpContext.Response.ContentType]);
                    context.HttpContext.Response.TransmitFile(filePath);
                }
            }
            catch (DirectoryNotFoundException)
            {
                if (this.FallbackPaths.ContainsKey(context.HttpContext.Response.ContentType))
                {
                    filePath =
                        context.HttpContext.Request.MapPath(
                            this.FallbackPaths[context.HttpContext.Response.ContentType]);
                    context.HttpContext.Response.TransmitFile(filePath);
                }
            }
        }
        public override void ExecuteResult(ControllerContext context)
        {
            var filePath = this.ViewName;
            var extIndex = filePath.LastIndexOf('.');

            if (extIndex > -1)
            {
                var fileExt = filePath.Substring(extIndex);
                if (!string.IsNullOrEmpty(fileExt) && ExtensionMapper.Contains(fileExt))
                {
                    context.HttpContext.Response.ContentType = ExtensionMapper.GetContentType(fileExt);
                }
            }

            base.ExecuteResult(context);
        }