Filename and mime problems - ASP.NET Download file (C#)

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 14:56:57
Chris Shouts

I think something like Response.Redirect(ResolveUrl(file.FullName)) instead of Response.TransmitFile(file.FullName) is what you intended. It sounds like you actually want their browser to point at the file, not just transmit the file as a response to the current the request.

Edit: Also see this SO question How to retrieve and download server files (File.Exists and URL)

Update: Based on your feedback, i think this is what you're looking for.

I'm looking at what I've done to handle a similar mechanism, and here's the steps I'm doing (bold item seemingly the only real difference):

Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // Excel 2007 format
// ... doing work...
Response.AddHeader("Content-Length", outputFileInfo.Length.ToString());
Response.TransmitFile(outputFileInfo.ToString());
HttpContext.Current.Response.End(); // <--This seems to be the only major difference

Although this.Response and HttpContext.Current.Response should be the same, it may not be for some reason.

For Excel export

   Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", fileName));

It worked for me with IE and Firefox.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!