I have a database file that looks like the following:
**Name Number1 Number2 File** Henk 123 456 c:henk.pdf Piet 345 789 c:piet.pdf
When I put this in a SQL Datasource (Visual Studio), the file should be clickable text or image. Now it’s just plain text.
<asp:BoundField DataField="Relatienummer" HeaderText="Relatienummer" SortExpression="Relatienummer" /> <asp:BoundField DataField="nummer" HeaderText="nummer" SortExpression="nummer" /> <asp:BoundField DataField="company" HeaderText="company" SortExpression="company" /> <asp:BoundField DataField="Link" HeaderText="Link" SortExpression="Link" />
What I currently have is:
<asp:HyperLinkField DataNavigateUrlFields="Link" DataNavigateUrlFormatString="Link" DataTextField="Link" HeaderText="Link" />
But this links to link instead of the content of the sql.
Thanks
Advertisement
Answer
Remember, code behind can use full file system – like any desktop code.
But if you going to use that file in the asp.net (web side), then you MUST be able to produce a valid URL path name.
So ALWAYS keep in mind the above:
Codebehind – path names are STANDARD windows file path names (and even “” etc.).
Web pages – markup = valid URL’s mapped by the web site.
If you confuse the above two concpets above? you be in for a world of pain by ignoring the above VERY simple idea.
So, you need TWO things to convert those path names to valid WEB links:
First up: The web server “site” needs rights and a “means” to point to a given folder in question.
So, you might have some say SAN or big network storage device – and you toss/place/have/put your files there.
Say like this:
\BIGSERVER1PdfFolderCustomers
Now in your code behind? You can use
\BIGSERVER1PdfFolderCustomersPolicy.pdf
But for the web server? Well, there is no WAY that you could and would allow just any old server path names on your network to be used by VALID WEB URL’s (again, that concept of code behind vs VALID web links MUCH be in your brain thoughts at ALL times).
Ok, so how would we use the above file path on the asp.net + web side? (now we NOT talking about code behind – right?).
Well, in 99% of cases, you thus fire up IIS management, and add what is called a Virtual folder. This thus mapps from the web site to the above folder.
It will look like this:
So in above, the folder UpLoadFiles will and can be mapped say this:
\BIGSERVER1PdfFolderCustomers
So, codebehind (your vb/c# code will use this:
\BIGSERVER1PdfFolderCustomersmyfile.pdf
But, a web link will be this
http://myCoolWebSite/UpLoadFiles/myfile.pdf
So any hyper link etc. HAS to be a valid URL as per above. And since we ONLY mapped the ONE folder to the web site, then users can’t just type in any old email and grab/get/mess with files on that server, or your whole netowrk for that matter. But KEEP in mind your code behind does not have such restrictions – that just plan jane code with plane jane valid full server or simple full valid windows pathnames.
So, in your data table, lets assume the file saved was
myfile.pdf
So, I do suggest that you separate out the valid windows path name from the file.
Ok, so to provide a valid web link, you need this expression:
"~/UpLoadFiles/" + "myfile.pdf"
So this can be a data bind expression, or even part of the SQL query that drives the grid/list or whoever it is displaying.
Last but not least:
Often in your code, you need (want/have to) translate a given valid URL to a correct good old plane jane file path name (after all ANY code behind will only work with those plane jane full valid windows path names.
So, your code can do this:
dim strFileName as string ' assume say you have the database and a row in a "data row" strFileName = Server.MapPath("~/UpLoadFiles/" & MyDataRow("FileName")) So above code will of course translate the virtual folder that points to the server holding the files and return a full valid windows path name for your code. (the same as the full server pathname I exampled above).
The other approach?
Well, don’t setup a virtual folder, and don’t allow valid URL paths directly to those files. If you dealing with say customer A files, and customer B files, then allowing valid URL’s to those files (that they COULD just type in), then you resort to ONLY allowing and use code behind to get those files.
So, now your hyper link becomes a button. When they click on that button, code behind reads the file and STREAMS (pumps) out the file to the web browser. They will thus get a standard file download when you do this, but NEVER does any actual valid URL exist for them to type in (and thus the files are secure – since then you and your code will fully control who can get what file since no valid URL’s that map to that folder question exist. Thus ONLY code behind can continue to use and enjoy plane jane valid windows file names, but the web side of things will not have nor allow valid URL’s to the mapped virtual folder, since you did not create one.
Of course if you create a “files folder” as a sub folder in the web site, then yes in most cases valid URL’s can be typed in. But if you use server or file folder OUTSIDE of the root web site and folders, then you have to use that virtual folder mapping to create and produce a value URL mapping to the web site.
So, if you want a hyper link? Then want a link button or some such.
iFile='<%# "~/UpLoadFiles/" + Eval("InternalFileName")%>'>
or say this for a hyper link:
src ='<%# "~/UpLoadFiles/" + Eval("InternalFileName")%>'>
So you have to setup a Virtual folder to map out the windows land plane jane folder to that of a valid web based url.
So you could even place that in the SQL query
SELECT ID, [Name], Number1, Number2, File, '~/UpLoadFiles/' + File as FileUrl FROM tblFiles
So, you now have a valid URL for that hyper link or link button.
You NOT shared how you setup file mapping for the web server, but just keep VERY clear in your mind that code behind STILL WILL ALWAYS use full standard path names. But the web site URL’s are not – they are based on the path name of your web site. As noted, you could just place the files in a sub-folder of the root of the web site you have, and then you don’t need a virtual folder. But for files outside of the site, or for security reasons, you often have the files and folders for these files NOT in the base web site folders. But adding a virtual (mapped) folder quite much means you can point/expose any valid folder system on the same network that the web server is running on.
I doubt that you allow URL’s to drive c:myfile.pdf.