Reputation: 71
I'm trying to redirect to the media url, but using the code blow, results in the full url concatenated by the size of the media.
So for example the url returns /media/123/example.pdfpdf4000
XPathNodeIterator xName = umbraco.library.GetMedia(Convert.ToInt32(pdfid), false);
string url = xName.Current.Value;
Response.Redirect("~/"+ url);
What is happening?
Upvotes: 2
Views: 4231
Reputation: 29484
To get the media file URL, it's simply
new Media(mediaId).getProperty("umbracoFile").Value.ToString()
Upvotes: 0
Reputation: 5526
I was just looking for how to do this, and umbraco now includes uQuery to get data like this easier, so to answer your question you could use:
Media pdf = umbraco.uQuery.GetMedia(pdfId);
Response.Redirect(pdf.GetProperty<string>("umbracoFile"));
Just a note: If it was an image media item (rather than a file) it'd be even easier:
Media img = umbraco.uQuery.GetMedia(imgId);
imgTag.ImageUrl = img.GetImageUrl();
Not sure why uQuery doesn't include GetFileUrl()
but GetProperty<string>()
seems nicer than using the xpath iterator yourself.
Upvotes: 3
Reputation: 10400
There is a solution explained here and also here. But, in essence you can create a method as below to fix the issue:
public string getImage(int ImageID)
{
XPathNodeIterator xn = umbraco.library.GetMedia(ImageID, false);
xn.MoveNext();
XPathNodeIterator xn2 = xn.Current.Select("data[@alias='umbracoFile']");
xn2.MoveNext();
return xn2.Current.Value;
}
Upvotes: 2