Get the absolute path in C# and bind the path dynamically to a image element using jquery
I want to display images I retrieve from the database.
In my database I have saved the absolute URL in the imagepath field and while I am retrieving I am getting the relative path with double slash instead. I want this '\'. how do I get the absolute path?
Another question is I have the image placeholder in my ASP.NET web page I want to set the imagepath with the path I retrieve from the database. How do I do that using jQuery or any other method??
I tried using this
$(document).ready(functi开发者_运维技巧on() {
$("#SecondAd1").attr("src", "C:\Users\Public\Pictures\Pictures\image.jpg");
});
But couldn't, how could I do this?
For your first question: If you want to replace in the server (C#):
string stringWithoutDoubleSlash = stringWithDoubleSlash.Replace(@"\\", @"\");
If you want to do it in the client (Javascript):
var stringWithoutDoubleSlash = stringWithDoubleSlash.replace(/\\/g, "\\");
For your second question, your code should be like this:
$(document).ready(function() {
$("#SecondAd1").attr("src", "C:\\Users\\Public\\Pictures\\Pictures\\image.jpg");
});
That's because '\'
is a javascript special character for escaping, so we have to prepend another '\'
.
Just a comment: You're using local paths, so your images will load only on your pc.
Hope this helps. Cheers.
u should store the retrieved images into a folder inside your website's root folder
精彩评论