Web Connection
Can't get the file name to download properly using response.transmitfile()
Gravatar is a globally recognized avatar based on your email address. Can't get the file name to download properly using response.transmitfile()
  Phil Sherwood
  All
  Jun 13, 2017 @ 03:09pm

I have a request that uses this url to download files from the site. It's the <a href= executed when the link on the page is clicked.

https://sherwaretest.onlinerpts.com/displayreport.or?filename=MR_109_ENERGY%20EXCEL%20IMPORT%20FILE_ENERGEN.XLSX

The DisplayReport method has this code:

lcExt = UPPER(JUSTEXT(lcFileName))
DO CASE 
   CASE lcExt = 'PDF'
      Response.transmitfile(lcFileName, 'application/pdf')
   CASE lcExt = 'DOC' 
      Response.transmitfile(lcFileName, 'application/msword')  
   CASE lcExt = 'DOCX' 
      Response.transmitfile(lcFileName, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document ')
   CASE lcExt = 'GIF'
      Response.transmitfile(lcFileName, 'image/gif')      
   CASE lcExt = 'JPEG' OR lcExt = 'JPG'
      Response.transmitfile(lcFileName, 'image/jpeg')      
   CASE lcExt = 'PNG'
     Response.transmitfile(lcFileName, 'image/png')     
   CASE lcExt = 'BMP'
      Response.transmitfile(lcFileName, 'image/bmp')            
   CASE lcExt = 'RTF'
      Response.transmitfile(lcFileName, 'application/rtf')
   CASE lcExt = 'TXT'
      Response.transmitfile(lcFileName, 'text/plain')   
   CASE lcExt = 'XLS' OR lcExt = 'XLSX'
      Response.transmitfile(lcFileName, 'application/msexcel')  
   CASE lcExt = 'XLSX'
      Response.transmitfile(lcFileName, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet          ')
   OTHERWISE
      response.transmitfile(lcFileName, 'text/plain')   
ENDCASE          

The probem I'm having is that when the file downloads, it has the displayreport.or filename instead of the actual filename. Is there a way to get it to respect the filename of the file being downloaded?

If I rename the file after it's downloaded it will open fine with Excel, Word or whatever the default application for the type is.

-Phil

Gravatar is a globally recognized avatar based on your email address. re: Can't get the file name to download properly using response.transmitfile()
  Rick Strahl
  Phil Sherwood
  Jun 13, 2017 @ 10:15pm

Use Response.DownloadFile() instead. TransmitFile() just sends the file, DownloadFile() sends the required headers to force the filename.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Can't get the file name to download properly using response.transmitfile()
  Phil Sherwood
  Rick Strahl
  Jun 14, 2017 @ 11:03am

Perfect!

Just what I needed. I misunderstood RESPONSE.DOWNLOADFILE. I thought it would always give a "Save As" dialog when downloading the file. Looks like it only does that if you don't give it the file name to use.

Thanks!

-Phil

© 1996-2024