2010-07-07 34 views
0

我正在根据用户选择来设置inlineattachment的处置方式。它工作但非常间歇。一旦我在同一个浏览器中选择内嵌相同的文件,它就会按预期工作。一旦我切换到附件,除非关闭并重新打开浏览器,否则我无法获得所需的结果。我确实清楚了上下文并且清除了它。C#中的内容处置问题

下面是我使用的代码:

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(); 
    } 

回答

0

它听起来像是一个浏览器缓存问题。尝试添加此:

Response.ExpiresAbsolute = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0)); 
Response.Expires = 0; 
Response.CacheControl = "no-cache"; 
+0

谢谢,这似乎工作,虽然我必须进一步测试。另外,为什么有些文件总是作为附件呈现,即使它们被指定为内联? – DotnetDude 2010-07-07 17:29:33

+0

如果浏览器不知道内容类型,因此不知道运行哪个应用程序将是一个附件下载 – 2010-07-07 17:45:46

0

你可能会发现,这是因为浏览器缓存响应,你可能需要调整在那个特定的行动缓存