Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 10, 2012 02:10 AM by aarsh
Dec 08, 2012 09:59 AM|LINK
I have a scenario in which I have a dynamic page into which is passed an ID value.
I use that value to create a download link for a PDF file that has the filename <ID>.pdf.
I would rather use more descriptive filenames for the PDF's such as <ID>_technical.pdf
(Just to be clear, instead of 23.pdf, I want 23_technical.pdf).
I can't use the filename in the string which identifies what PDF to download so I am faced with needing some C# which (pseudo code) says:
"download the PDF with filename that STARTS with <ID>.
I need to just grab the few characters up to - but not including - the underscore.
The <ID> may be two or three characters long.
Is this possible?
Dec 08, 2012 01:59 PM|LINK
Dec 08, 2012 02:29 PM|LINK
i'm not exactly sure what you are asking here, so i'm making some assumptions, which are:
(a) you have a fixed number of files on your server,
(b) those files are all in the same location, i.e., directory,
(c) their filenames all have the format: nnn_aaaaaa...aaa.pdf,
(d) your end user is only passing you the nnn part (perhaps by clicking a link?),
(e) you will let your end user download the file that is associated with the
TIMTOWTDI =. there is more than one way to do it
from what little i think i know, based on my assumptions above, if there are not a large number of files, and the files do not change often, i might use a
Dictionary<Int32, String> which would contain entries like:
Then you simply look up your document.
if your files are in different directories, you could store the full path in the dictionary instead of simply the filename.
if you have gazillions of files, you could store the filenames (or full paths) in a database and do a lookup against your database.
Dec 08, 2012 08:59 PM|LINK
First get a DirectoryInfo object for the directory where the files are stored then use the GetFiles method to find your file. Something like
var pdfHomeDir = new DirectoryInfo(Server.MapPath("/MywebSite/pdfFolder"));
FileInfo desiredPDF = pdfHomeDir.GetFiles(theCharactersThatYouKnow + "_*.pdf").FirstOrDefault();
string desiredPDFFIleName = desiredPDF.FullName;
Dec 10, 2012 02:10 AM|LINK
After reading your question several times I ended up thinking in a scenario as below:
I hope this helps you (if I could have understood your question properly) and if not please post ...