473,473 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I change the name of file being downloaded

7 New Member
I'm new to HTML.

What is the HTML <a href> code/syntax to download a file to a different name than what is on the server?

I have many files stored on the server with cryptic file names, but I would like the files to be downloaded and saved with a "friendly name".

Is there a way to do this just by using the <a href> command?
Oct 22 '07 #1
12 3762
drhowarddrfine
7,435 Recognized Expert Expert
html cannot do this. In fact, it is not a function of the browser at all.
Oct 22 '07 #2
JamieHowarth0
533 Recognized Expert Contributor
Hi coetzecc,

If you are using ASP or PHP to serve up your file, you can use HTTP headers to add the following info:
Expand|Select|Wrap|Line Numbers
  1. Content-Disposition:attachment;filename:yourfile.ext
.

If you have no idea what PHP or ASP are then let us know and we might be able to help you further.

Best regards,

medicineworker
Oct 22 '07 #3
coetzecc
7 New Member
hi medicineworker,

Thanks for your reply.

Let me give you a bit more info.

I'm trying to extract files form WSUS. I found a utility on the web to do exactly that. Now I'm trying to improve it a little.

The page is written in ASP. It uses SQL to query a database and returns one or more results. I am then able to download the file but the filename is long and cryptic.

The search results include the long cryptic name plus the "friendly name".
I'd like to use that friendly for the downloads instead of the cryptic name.

Here is a excerpt of the ASP code:

Expand|Select|Wrap|Line Numbers
  1. Dim objConn, sqlString
  2.  Set objConn = Server.CreateObject("ADODB.Connection")    
  3.  objConn.Open "HotfixInfo"
  4.  
  5.  Dim rs
  6.  sqlString = "SELECT FileName, MUURL, FileDigest " & _
  7.             "FROM tbFile " & _
  8.           "WHERE FileName like '%" & sSearch & "%' " & _
  9.             "and FileName not like '%psf%' " & _
  10.             "and FileName not like '%express%' " & _
  11.           "ORDER BY FileName"
  12.  
  13.  set rs = objConn.Execute(sqlString)
  14.  
  15.  If Not rs.eof Then
  16.   Do While Not rs.eof 
  17.    Dim sFileName, sFileLocation, sFourtyChr, sFileDir,sFileDirFull, sFileFull, sFileWeb
  18.      sFileName     = rs(0)
  19.      sFileLocation = rs(1)
  20.      sFourtyChr    = right(sFileLocation, 44)        
  21.      sFileDir      = right(sFileLocation, 6)
  22.      sFileDir      = left(sFileDir, 2)
  23.      sFileDirFull  = sWSUSContentDir+sFileDir
  24.      sFileFull     = sFileDirFull+"\"+sFourtyChr
  25.      sFileWeb      = sWSUSContentSrv+sFileDir+"/"+sFourtyChr
  26.  
  27.      'Checks if the hotfix file exist
  28.      If (fso.FileExists(sFileFull)) Then
  29.        Dim sLink
  30.        sLink = ("<a href="""+sFileWeb+""">"+sFileName+"</a>"
  31.        %>
  32.        <tr>
  33.             <td colspan="2" class="content"><%= sLink %></td>
  34.             <td colspan="2" class="content"><%= sFourtyChr %></td>
  35.             <td colspan="2" class="content"><%= sFileDigest %></td>
  36.        </tr>
  37.        <%
  38.      End If
  39.      rs.MoveNext
  40.    Loop
  41.  Else
  42.    %>
  43.      <tr>
  44.         <td colspan="6" class="content" align="center"><br>
  45.             <h2>There are no files with the search string [<%= sSearch %>] available<br>
  46.                 Tip: Search with the KB article number.</h2><br>          
  47.      </td>
  48.      </tr>
  49.    <%
  50.  End If
I'm not sure where to insert you suggested code.
Any suggestions?
Oct 22 '07 #4
drhowarddrfine
7,435 Recognized Expert Expert
Since this is an asp question, you would get more help on that board, where I can transfer this, but I need to know if it's ASP or ASP.NET.
Oct 22 '07 #5
coetzecc
7 New Member
Since this is an asp question, you would get more help on that board, where I can transfer this, but I need to know if it's ASP or ASP.NET.

Well WSUS setup requires ASP to be installed on the server. So I presume it's ASP.
Oct 23 '07 #6
coetzecc
7 New Member
Can anyone help me with this, please?
Oct 23 '07 #7
JamieHowarth0
533 Recognized Expert Contributor
Hi coetzecc,

I am unfortunately a bit busy today working on other projects but I will look into your query tomorrow morning and work out some kind of solution (which will involve an intermediary ASP file to parse the filenames from the WSUS database).

Best regards,

medicineworker
Oct 23 '07 #8
coetzecc
7 New Member
Hi coetzecc,

I am unfortunately a bit busy today working on other projects but I will look into your query tomorrow morning and work out some kind of solution (which will involve an intermediary ASP file to parse the filenames from the WSUS database).

Best regards,

medicineworker

Thanks medicineworker. (eagerly awaiting your reply)
Oct 24 '07 #9
JamieHowarth0
533 Recognized Expert Contributor
Hi coetzecc,

This is the code provision I came up with. Notice the comments (marked here in
Expand|Select|Wrap|Line Numbers
  1. 'green
) which explains what I have done and how I have done it.

This is your current code:
Expand|Select|Wrap|Line Numbers
  1. Dim objConn, sqlString
  2. Set objConn = Server.CreateObject("ADODB.Connection") 
  3. objConn.Open "HotfixInfo"
  4.  
  5. Dim rs
  6. sqlString = "SELECT FileName, MUURL, FileDigest " & _
  7. "FROM tbFile " & _
  8. "WHERE FileName like '%" & sSearch & "%' " & _
  9. "and FileName not like '%psf%' " & _
  10. "and FileName not like '%express%' " & _
  11. "ORDER BY FileName"
  12.  
  13. set rs = objConn.Execute(sqlString)
  14.  
  15. If Not rs.eof Then
  16. Do While Not rs.eof 
  17. Dim sFileName, sFileLocation, sFourtyChr, sFileDir,sFileDirFull, sFileFull, sFileWeb
  18. sFileName = rs(0)
  19. sFileLocation = rs(1)
  20. sFourtyChr = right(sFileLocation, 44) 
  21. sFileDir = right(sFileLocation, 6)
  22. sFileDir = left(sFileDir, 2)
  23. sFileDirFull = sWSUSContentDir+sFileDir
  24. sFileFull = sFileDirFull+"\\"+sFourtyChr
  25. sFileWeb = sWSUSContentSrv+sFileDir+"/"+sFourtyChr
  26.  
  27. 'Checks if the hotfix file exist
  28. If (fso.FileExists(sFileFull)) Then
  29. Dim sLink
  30. 'Added link to ASP file (code further down) which will parse your link value and rename the "attachment" as such into a friendly name
  31. sLink = ("<a href=""filedownloader.asp?fn="+sFileWeb+"&ffn="+sFileName+""">"+sFileName+"</a>"
  32. %>
  33. <tr>
  34. <td colspan="2" class="content"><%= sLink %></td>
  35. <td colspan="2" class="content"><%= sFourtyChr %></td>
  36. <td colspan="2" class="content"><%= sFileDigest %></td>
  37. </tr>
  38. <%
  39. End If
  40. rs.MoveNext
  41. Loop
  42. Else
  43. %>
  44. <tr>
  45. <td colspan="6" class="content" align="center"><br>
  46. <h2>There are no files with the search string [<%= sSearch %>] available<br>
  47. Tip: Search with the KB article number.</h2><br> 
  48. </td>
  49. </tr>
  50. <%
  51. End If
  52.  
Code for filedownloader.asp:
Expand|Select|Wrap|Line Numbers
  1. <%@Language="VBScript"%>
  2. <%Option Explicit%>
  3. <%Response.Buffer = True%>
  4. <%
  5. On Error Resume Next
  6. Dim strPath
  7. 'Get web path to file from "fn" QueryString
  8. strPath = CStr(Request.QueryString("fn"))
  9. '-- do some basic error checking for the QueryString
  10. If strPath = "" Then
  11. Response.Clear
  12. Response.Write("No file specified.")
  13. Response.End
  14. ElseIf InStr(strPath, "..") > 0 Then
  15. Response.Clear
  16. Response.Write("Illegal folder location.")
  17. Response.End
  18. ElseIf Len(strPath) > 1024 Then
  19. Response.Clear
  20. Response.Write("Folder path too long.")
  21. Response.End
  22. Else
  23. Call DownloadFile(strPath)
  24. End If
  25.  
  26. Private Sub DownloadFile(file)
  27. '--declare variables
  28. Dim strAbsFile
  29. Dim strFileExtension
  30. Dim objFSO
  31. Dim objFile
  32. Dim objStream
  33. '-- set absolute file location
  34. strAbsFile = Server.MapPath(file)
  35. '-- create FSO object to check if file exists and get properties
  36. Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  37. '-- check to see if the file exists
  38. If objFSO.FileExists(strAbsFile) Then
  39. Set objFile = objFSO.GetFile(strAbsFile)
  40. '-- first clear the response, and then set the appropriate headers
  41. Response.Clear
  42. '-- the filename you give it will be the one that is shown
  43. ' to the users by default when they save
  44. ' med: added "friendly" file name capability (passed from "ffn" QueryString)
  45. Response.AddHeader "Content-Disposition", "attachment; filename=" & Request.QueryString("ffn")
  46. Response.AddHeader "Content-Length", objFile.Size
  47. Response.ContentType = "application/octet-stream"
  48. Set objStream = Server.CreateObject("ADODB.Stream")
  49. objStream.Open
  50. '-- set as binary
  51. objStream.Type = 1
  52. Response.CharSet = "UTF-8"
  53. '-- load into the stream the file
  54. objStream.LoadFromFile(strAbsFile)
  55. '-- send the stream in the response
  56. Response.BinaryWrite(objStream.Read)
  57. objStream.Close
  58. Set objStream = Nothing
  59. Set objFile = Nothing
  60. Else 'objFSO.FileExists(strAbsFile)
  61. Response.Clear
  62. Response.Write("No such file exists.")
  63. End If
  64. Set objFSO = Nothing
  65. End Sub
  66. %>
  67.  
Note that this code was taken and modified from
http://www.xefteri.com/articles/show.cfm?id=7 and they deserve
the credit for this code - I simply modified it for your usage.

Hope it helps.

Best regards,
medicineworker
Oct 24 '07 #10
coetzecc
7 New Member
Thank you so much medicineworker.
I will try to implement your code and let you know if I get it to work.
Oct 24 '07 #11
coetzecc
7 New Member
IT WORKS!!!
Thanks a million medicineworker! You're the MAN!
Oct 25 '07 #12
JamieHowarth0
533 Recognized Expert Contributor
You're very welcome :-)
Any further questions on this subject, you are welcome to PM me anytime.

medicineworker
Oct 26 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Reply Via Newsgroup | last post by:
Folks, Say I have a table, ten columns, ten rows - Each with a word in it. I want to change the values of some/all of the cells in the table via a hyperlink. How do I reference each cell and...
1
by: Steve Mauldin | last post by:
I am having an issue with Mac IE users that come to an asp page on my site. The page does a response.redirect to a PDF page. the PDF is downloaded onto the desktop and saved as the name of the ASP...
5
by: who be dat? | last post by:
Hello all. I'm writing an application that is writing trace information that can be viewed in trace.axd. I would like to rename this and use a different name specific to my application. I know...
0
by: ProJee | last post by:
Hi, Response.WriteFile (or Response.OutputStream.Write) finishes immediately, not after the file is completely downloaded. It finishes before (!) the user clicks the "Save" or "Open" browser...
6
by: balakrishnan.dinesh | last post by:
hi frnds I need to change the Browser settings dynamically in Onload event through javascript. Like Goto: Tools->Internet Options -General -Settings -Check for newer Versions of stored pages....
6
by: Daniel Padron | last post by:
Ok. Maybe I shouldnt post such basic questions here in such an advanced group but my high school programming teacher wont answer any questions outside of his curriculum :( My goal is create a...
7
by: lawrence k | last post by:
I've got a music studio for a client. Their whole studio is run with Macintosh computers. Macintosh computers allow file names to have open white spaces, such as "animal hospital.mp3". I have a...
6
by: Andrew Jocelyn | last post by:
Hi How do I programmatically change (read/write) the values in this app.config file at runtime? Specifically I want to change the client endpoint address but it would be nice to change other...
8
by: Mateusz Viste | last post by:
Hi, I'm not sure if my question is really related to JavaScript, so please excuse me if that's not the case (and maybe you guys would have an idea what's the cause is and where could I ask)... ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.