How to get Sharepoint image in relative url format - c#

I have an SPListItem that I return a full image url:
http://sharepointsite.com/images/bob's picture.jpg
I return this url by calling this:
splistitem.File.ServerRelativeUrl
I want to be able to turn this url into this:
http://sharepointsite.com/images/bob%27s%20picture.jpg
but if I encode the full URL it will replace the / which i dont want. I want to be able to just get the ending image file and UrlEncode that, how would I go about solving this progmmatically?

Try this:
var url = "http://sharepointsite.com/images/bob's picture.jpg";
var basePath = System.IO.Path.GetDirectoryName(url);
var fileName = System.IO.Path.GetFileName(url);
var finalPath = basePath + "\\" + Uri.EscapeDataString(fileName);

You can achieve this by simply calling splistitem.File[SPBuiltInFieldId.EncodedAbsUrl] - it won't replace the forward-slashes. And then you can pass it to System.IO.Path.GetFileName if you want only the filename.
By the way, shouldn't ServerRelativeUrl return /images/bob's picture.jpg?

Related

get amazon web service unsigned URLs in c#

I m using amazon sdk for .net
i have uploaded a file in folder of my bucket , now i want to get the url of that file using this code
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
request.BucketName = "my-new-bucket2";
request.Key = "Images/Tulips.jpg";
request.Expires = DateTime.Now.AddHours(1);
request.Protocol = Protocol.HTTP;
string url = s3.GetPreSignedURL(request);
but this is returning url with key , exipration date and signature but infact i want to get the url without them , there is no other method to get the url
**Things i tried **
i search and found that i have to change permission of my file
i have change the permission of file while uploading
request.CannedACL = S3CannedACL.PublicRead;
but still its returning the same url
http://my-new-bucket2.s3-us-west-2.amazonaws.com/Images/Tulips.jpg?AWSAccessKeyId=xxxxxxxxxxxx&Expires=1432715743&Signature=xxxxxxxxxxx%3D
it work when i remove keyid ,expire and signature
but how can i get url with out it , or do i have to do it manually
This is by design. If you know the bucket name and the key then you have everything you need to construct the URL. As an example, here bucketname is yourbucketname and the key is this/is/your/key.jpg.
https://yourbucketname.s3.amazonaws.com/this/is/your/key.jpg
Hope that helps!
I just browsed their documentation and was not able to find a method to return an absolute url. However, I really believe there is one that I could not see. For now, you can solve your problem by extracting an absolute url from the result you have:
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
request.BucketName = "my-new-bucket2";
request.Key = "Images/Tulips.jpg";
request.Expires = DateTime.Now.AddHours(1);
request.Protocol = Protocol.HTTP;
string url = s3.GetPreSignedURL(request);
index = url.IndexOf("?");
if (index > 0)
string absUrl = url.Substring(0, index);
Hope it helps :)

Extracting the Portion of the URL in C#

I have a URL like this:
http://www.example.com/Catalog/Category/Detail
http://www.example.com/Catalog/Products/12
Now, I want to extract the /Catalog/Category/Detail and /Catalog/Products/12 part so I can append with some other base url. How can I do that easily?
Use Uri class, and use Uri.LocalPath property like:
Uri uri = new Uri("http://www.example.com/Catalog/Category/Detail");
Console.WriteLine(uri.LocalPath); // /Catalog/Category/Detail
var segments = new Uri("http://www.example.com/Catalog/Category/Detail").Segments;
This will return
/
Catalog/
Category/
Detail

redirection to other page with encoded url

I am using one Image on my html page. where i am adding one anchor to that image to give the link.
that anchor contains href as ~/LB/lct.aspx?pid=177&cat=Happily In Love
Main Thing This URL IS COMING FROM DATABASE. Manually i am not entering it..
So its a Invalid URL Because of spaces between Happily In Love
I used Httputility.urlencoding and decoding also........but the problem i am facing is that..
Url is endoded properly but while i am clicking on the image its not redirecting to proper page because encoded url is not decoded..
How to resolve this...pls help me on this....
Here is the code
string url = "~/LB/lct.aspx?pid=177&cat=Happily In Love"; //your input
string[] arr = url.Split('?');
var nameValues = HttpUtility.ParseQueryString(arr[1]);
foreach (var n in nameValues.AllKeys)
{
nameValues.Set(n, HttpUtility.UrlEncode(nameValues[n]));
}
url = arr[0] + "?" + nameValues.ToString(); //your output
Use the following code to decode Querystring values
string cat = HttpUtility.UrlDecode(Request.QueryString["cat"].ToString());
Simple answer, but if it's just the spaces that are causing problems, consider replacing them with a + using a string replace:
string newurl = url.Replace(" ","+");
Note that this is only really safe if the spaces are restricted to the contents of a querystring.

Input URL like http://example.com changes to http:/example.com in action input

I asked a question to get URL as action input here. Now I have a new problem. The passed URL to action changes from http://example.com to http:/example.com.
I want to know why and how can I resolve the problem.
P.S: I added this code to resolve but I think there may be another problems in future! the code is:
if ((url.Contains(":/")) && !(url.Contains("://")))
{
url = url.Replace(":/", "://");
}
The browser (or server) is replacing a double slash (illegal) with a single one.
Try it,
http://stackoverflow.com/questions/11853025//input-url-like-http-site-com-changes-to-http-site-com-in-action-input
(in Chrome) goes to:
http://stackoverflow.com/questions/11853025/input-url-like-http-site-com-changes-to-http-site-com-in-action-input
If I were you, I would remove the http:// from your path and add it later.
http://localhost:1619/Utility/PR/example.com/
Then, url = "http://" + url;
If you might get secure urls, add that to the route /http/example.com or /https/example.com
use regex:
string src = #"http://example.com";
string result = Regex.Replace(src, #"(?<=https?:/)/", "");
if you need to revert:
string src = #"http:/example.com";
string result = Regex.Replace(src, #"(?<=https?:)/(?=[^/])", #"//");

C# I want the url, not the physical pathname

For this line of code;
string link = HttpContext.Current.Server.MapPath("/Contract/Details/" + this.ContractId.ToString());
I get the physical pathname on C drive.
What I want is the url, ie
http://localhost:1234/Contract/Details/1
How do I get this?
// Use the Uri constructor to form a URL relative to the current page
Uri linkUri = new Uri(HttpContext.Current.Request.Url, "/Contract/Details/" + this.ContractId.ToString());
string link = linkUri.ToString();
try this:
string url = HttpContext.Current.Request.Url.AbsoluteUri;
There's a great article on .Net paths # http://west-wind.com/weblog/posts/132081.aspx
Take a look at the Url or PathInfo property.
Uri base = new Uri("http://localhost:1234/";);
Uri file = new Uri(host, "/Contract/Details/" + this.ContractId.ToString());
string URL = file.AbsoluteUri;

Categories

Resources