HttpResponse.WriteFile and the file name.

The problem, your user uploads a file, 'aFile.txt', to your web site, the web site processes it and generates another file, which you temporary save under the filename 'tmp789.tmp'. You then send that temporary file to your user as a download, but you don't want it to be named tmp789.tmp. So how do you change the download name of the file being sent using HttpResponse.WriteFile()?

Here's how....


HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + myNewFileName));
HttpContext.Current.Response.ContentType = "application/xml";
HttpContext.Current.Response.WriteFile(pathToFileBeingDownload);
HttpContext.Current.Response.End();

Be sure and set the ContentType and also make sure you call the HttpResponse.End() method!

Published Saturday, January 27, 2007 5:32 AM by dsmyth
Filed under:

Comments

No Comments