views:

50

answers:

2

I am setting the disposition of either inline or attachment based on users selection. It works but very intermittently. Once I select inline for the same file in the same browser, it works as expected. Once I switch to attachment, I don't get the desired results unless I close and reopen the browser. I do flush the context and clear it as well.

Here's the code I am using:

    public void GetResult(ControllerContext context)
    {
        context.HttpContext.Response.Buffer = true;
        context.HttpContext.Response.Clear();
        context.HttpContext.Response.ContentType = ContentType;

        ContentDisposition disposition = new ContentDisposition();
        disposition.FileName = FileName;
        disposition.Inline = this.ContentDispositionType == ContentDispositionType.Inline ? true : false;

        context.HttpContext.Response.AddHeader("content-disposition", disposition.ToString());
        context.HttpContext.Response.WriteFile(context.HttpContext.Server.MapPath(Path));

        context.HttpContext.Response.Flush();
        context.HttpContext.Response.End();
    }
A: 

You'll likely find that it is because the browser is caching the response, you may want to adjust the caching on that particular action

Matthew Abbott
A: 

It sounds like a browser caching issue. Try adding this:

Response.ExpiresAbsolute = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0)); 
Response.Expires = 0; 
Response.CacheControl = "no-cache";
etc
Thanks, that seems to work, although I have to test it further. Also, why is it that some files are always rendered as attachments even if they are specified as inline?
DotnetDude
if the browser doesn't know the content-type thus not knowing which application to run it will be an attachment for download
matt-dot-net