tjgrindsted
asked on
How do i show the newest pdf file from folder as a link !?
Hi
I have a folder "upload" here i have some PDF files, i can upload a file to the folder and the files is getting the upload date as name, so i file uploaded today will have the name "20111207.pdf"
I need to make a link that only link to the newest PDF file.
So i dont need to changes the link when i have uploadet a PDF file, but so i only need to upload the pdf with the date and then a script will find the file with the biggest number and show that as a link.
How can i do that !?
ex.
i god these files in the folder.
Upload
- 20101015.pdf
- 20110125.pdf
- 20120101.pdf
then i need a script that can look after the highest number (in this case 20120101)
and then make a link to that file as <a href="upload/20120101.pdf" >Hent Prisliste</a>
and if i upload a new pdf file with at bigger number then 20120101 then it vil show that link, to the file.
Hope u understand what im looking for.
I have a folder "upload" here i have some PDF files, i can upload a file to the folder and the files is getting the upload date as name, so i file uploaded today will have the name "20111207.pdf"
I need to make a link that only link to the newest PDF file.
So i dont need to changes the link when i have uploadet a PDF file, but so i only need to upload the pdf with the date and then a script will find the file with the biggest number and show that as a link.
How can i do that !?
ex.
i god these files in the folder.
Upload
- 20101015.pdf
- 20110125.pdf
- 20120101.pdf
then i need a script that can look after the highest number (in this case 20120101)
and then make a link to that file as <a href="upload/20120101.pdf"
and if i upload a new pdf file with at bigger number then 20120101 then it vil show that link, to the file.
Hope u understand what im looking for.
You can use a LINQ query, to get the MAX date, and use that to generate a HyperLink control.
Try following
<a href='<%# GetLink() %>'>Download PDF</a>
Add this function to the code behind
Private Function GetLink() As String
Dim directory = New DirectoryInfo("C:\MyDirect ory")
Dim myFile = (from f in directory.GetFiles() Order By f.LastWriteTime Descending).First()
return myfile
End Function
You may want to place with the myfile path to make sure you return correct relative URL.
<a href='<%# GetLink() %>'>Download PDF</a>
Add this function to the code behind
Private Function GetLink() As String
Dim directory = New DirectoryInfo("C:\MyDirect
Dim myFile = (from f in directory.GetFiles() Order By f.LastWriteTime Descending).First()
return myfile
End Function
You may want to place with the myfile path to make sure you return correct relative URL.
ASKER
Hi all sry, that im writing back now.
@CodeCruiser
if i use ur ex. and add Import System.IO on the codebehind
I get this error in live code.
BC30390: U cant get access to '_Default.Private Function GetLink() As String' when its 'Private'.
@CodeCruiser
if i use ur ex. and add Import System.IO on the codebehind
I get this error in live code.
BC30390: U cant get access to '_Default.Private Function GetLink() As String' when its 'Private'.
ASKER
If i changes the Private Function to Public, then i get this error
BC30311: A value of the type 'System.IO.FileInfo' can nok be convertet to 'String'.
BC30311: A value of the type 'System.IO.FileInfo' can nok be convertet to 'String'.
Change this
return myfile
to
return myfile.FullName
return myfile
to
return myfile.FullName
ASKER
Hi again
If i add
Import System.OI
and changes the Privat to Public and add the code u just give me, and i then run the code
i then get this link, when i make a mouse over the "download PDF"
http://localhost:1145/ShowNewestFileFromFolder_OK/Default.aspx
Where it need to be
http://localhost:1145/ShowNewestFileFromFolder_OK/upload/WEB_X6DVBEUA-ENG-0401.pdf
right now its the only file in the Upload folder, so it must be the newest.
If i add
Import System.OI
and changes the Privat to Public and add the code u just give me, and i then run the code
i then get this link, when i make a mouse over the "download PDF"
http://localhost:1145/ShowNewestFileFromFolder_OK/Default.aspx
Where it need to be
http://localhost:1145/ShowNewestFileFromFolder_OK/upload/WEB_X6DVBEUA-ENG-0401.pdf
right now its the only file in the Upload folder, so it must be the newest.
ASKER
Code
Imports System.IO
Partial Class _Default
Inherits System.Web.UI.Page
Public Function GetLink() As String
Dim directory = New DirectoryInfo("C:\Users\MacNitro\Documents\Visual Studio 2010\WebSites\ShowNewestFileFromFolder_OK\Upload")
Dim myFile = (From f In directory.GetFiles() Order By f.LastWriteTime Descending).First()
Return myFile.FullName
End Function
End Class
If you put a break point, what is myfile.fullname returning?
ASKER
Breakline.... Do i add that before the return myfile or at the return myFile or is it on the maste codepage i add it !?
You can put the breakpoint on return statement and see the value of myfile.fullname
ASKER
ok
When i add the BP and run the code, and i then look at the BreakPoint window, i see
Function
_Default.GetLink()
Process
WebDev.WebServer40.exe
Condition
no condition
When i add the BP and run the code, and i then look at the BreakPoint window, i see
Function
_Default.GetLink()
Process
WebDev.WebServer40.exe
Condition
no condition
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
then i get this, have tryed 2 times
?myfile.fullname
The expression cannot be evaluated while in run mode.
?myfile.fullname
The expression cannot be evaluated while in design mode.
?myfile.fullname
The expression cannot be evaluated while in run mode.
?myfile.fullname
The expression cannot be evaluated while in design mode.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i cant get this to work, im sry to say it, and yes maybe its me, but im not coding as a pro. so i need the knowhow and exp. to know alot of things, but what u are writing, im not getting it, sry...
ASKER
dident get it to work.
You may want to clarify how and where this script is to run, what language you are after and the like though it isn't my area now anyway.
Steve