Relative path to documents, passed from href to Javascript function, getting back slashes and period stripped out

Thursday, July 10, 2014

I have an aspx page with an asp:GridView control that lists documents that have been uploaded pertaining to a legal case. The first column in the GridView is a series of href tags that, if clicked on, will open the document in the default program associated with that document type. The relative path to the document is stored in SQL Server. I use the DataBinder.Eval method to populate the path, which is then sent to a Javascript function that opens the document. By example, I have one document record whose path is DATA\AR000001\AttachFiles\Case11\2.txt. The markup for the anchor in the grid is as follows:



<ItemTemplate>
<a href="http://ift.tt/Pi2lUG" onclick="ViewAttachFile('<%#Databinder.Eval(Container.DataItem, "FullAttchPath") %>');">Open</a>
</ItemTemplate>


The Javascript function is:



function ViewAttachFile(sFile) {
docs = window.open(sFile);
}


If I view the page source, the relative path looks exactly as I would expect:



<a href="http://ift.tt/Pi2lUG" onclick="ViewAttachFile('DATA\AR000001\AttachFiles\Case11\2.txt');">Open</a>


Setting a breakpoint at ViewAttachFile, however, I find that the following is being passed to the function, note all backslashes and the document name have been stripped:


DATAAR000001AttachFilesCase11.txt (this is from the text visualizer)


I have tried embedding the escape and replace functions around the DataBinder.Eval call, but with no success, as Javascript is not my strong suit, not that I'm sure either of these would be the right solution anyway. I have also tried bypassing the ViewAttachFile function altogether, and coding the link as:



<a href="http://ift.tt/Pi2lUG" onclick="javascript:window.open('<%#Databinder.Eval(Container.DataItem, "FullAttchPath") %>');">Open</a>


but that essentially yields the same result, as the backslashes are stripped out of the URL, the period is converted to %02 and the window opened returns a 400 error for Bad Request - Invalid URL. I supposed I could double-up on the slashes in the path column in the database, but that seems like a bad idea, and I'm hoping for a cleaner resolution.


Any insight is greatly appreciated!







http://ift.tt/1qZPw2V