0

I have an image in which click is bind to open some url in other window. The image is like

<img id="ImgDPreview" src="../images/preview/thumbnail_pdf.jpg" style="border-width:0px;">

What below is a C# code snippet.

string PdfFilePath="\\\\192.23.8.19\\ArvinsPC\\MyFolders\\Original\\69a03d19-4b3d-41bc-9dcf-02982a1468df.pdf";
ImgDPreview.Attributes.Add("onclick", "return PreviewFiles('" + PdfFilePath + "');");

After running this, ImgDPreview is like

<img id="ImgDPreview" onclick="return PreviewFiles('\\192.23.8.19\ArvinsPC\MyFolders\Original\69a03d19-4b3d-41bc-9dcf-02982a1468df.pdf');" src="../images/preview/thumbnail_pdf.jpg" style="border-width:0px;">

function PreviewFiles(FilePath){
   //FilePath here becomes-->'\\192.23.8.19ArvinsPCMyFoldersOriginal69a03d19-4b3d-41bc-9dcf-02982a1468df.pdf'     
   window.open(FilePath);
}

The issue is on each step a backslash is being vanished from the URL. I know the javascript consider backslash as an escape character. But I need my URL unaltered. Is there any resolution for this.

URL and function names are fictional

Arvin
  • 954
  • 3
  • 14
  • 33

1 Answers1

1

Assuming that 192.23.8.19/ArvinsPC/MyFolders/Original/69a03d19-4b3d-41bc-9dcf-02982a1468df.pdf is accessible from browser. You need to do the following

  1. prepend the protocol to it to make it a complete URL
  2. Remove \\ from the URL
  3. Replace \ with /

Code

 function PreviewFiles(FilePath){
   FilePath = "https://" + FilePath.replace(/\\\\/g, "").replace(/\\/g, "/");
   window.open(FilePath);
 }

or as suggested by @slyhuy

window.open(FilePath.replace(/\\/gi, '/'));

I assumed that you are looking for https, you can replace it with the protocol you are using.

If the URL is not accessible from browser, you need to write a service on the server side to accept this FilePath from browser and return you the image.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94