I just got my hands on some code snippet that I found here on the forum in an old post. The purpose is to create a Media from the path of a FileUpload control and return the media. I would like to credit the guy who made this code, but I simply can't remember his name :/
Anyway, when I run the code it gives me the error "Access to path 'the id of media node' denied" It does make the actual media node in the Media content tree, but there's no data in it.I have placed a key in my web.config under appSettings to point to the media folder of my site: D:/WEB/mysite.dk/www/media, which I believe is the right path (got it from the Server.MapPath("."); method)
Anyone got this problem before and know of a workaround?
The code to create the media item is:
public Media CreateMedia()
{
string mediaPath = "";
Media m = null;
if (ulImage.PostedFile != null)
{
if (ulImage.PostedFile.FileName != "")
{
// Find filename
string _text = ulImage.PostedFile.FileName;
string filename;
string _fullFilePath;
filename = _text.Substring(_text.LastIndexOf("\\") + 1, _text.Length - _text.LastIndexOf("\\") - 1).ToLower();
// create the Media Node
// TODO: get parent id for current category - as selected by user (see below)
// - for now just stick these in the media root :: node = -1
m = Media.MakeNew(
filename, MediaType.GetByAlias("image"), User.GetUser(0), 1211);
// Create a new folder in the /media folder with the name /media/propertyid
string mediaRootPath = HttpContext.GetGlobalResourceObject("AppSettings", "MediaFilePath") as string; // get path from App_GlobalResources
string storagePath = mediaRootPath + m.Id.ToString();
System.IO.Directory.CreateDirectory(storagePath);
_fullFilePath = storagePath + "\\" + filename;
ulImage.PostedFile.SaveAs(_fullFilePath);
// Save extension
string orgExt = ((string)_text.Substring(_text.LastIndexOf(".") + 1, _text.Length - _text.LastIndexOf(".") - 1));
orgExt = orgExt.ToLower();
string ext = orgExt.ToLower();
try
{
m.getProperty("umbracoExtension").Value = ext;
}
catch { }
// Save file size
try
{
System.IO.FileInfo fi = new FileInfo(_fullFilePath);
m.getProperty("umbracoBytes").Value = fi.Length.ToString();
}
catch { }
// Check if image and then get sizes, make thumb and update database
if (",jpeg,jpg,gif,bmp,png,tiff,tif,".IndexOf("," + ext + ",") > 0)
{
int fileWidth;
int fileHeight;
FileStream fs = new FileStream(_fullFilePath,
FileMode.Open, FileAccess.Read, FileShare.Read);
System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
fileWidth = image.Width;
fileHeight = image.Height;
fs.Close();
try
{
m.getProperty("umbracoWidth").Value = fileWidth.ToString();
m.getProperty("umbracoHeight").Value = fileHeight.ToString();
}
catch { }
// Generate thumbnails
string fileNameThumb = _fullFilePath.Replace("." + orgExt, "_thumb");
generateThumbnail(image, 100, fileWidth, fileHeight, _fullFilePath, ext, fileNameThumb + ".jpg");
image.Dispose();
}
mediaPath = "/media/" + m.Id.ToString() + "/" + filename;
m.getProperty("umbracoFile").Value = mediaPath;
m.XmlGenerate(new XmlDocument());
}
}
// return the media...
return m;
}
User.GetUser(0) is unlikely to throw an error, if you don't have a user ID of 0 a lot of Umbraco will error, I'd be looking at the MediaType.GetByAlias.
With your initial question I'm sure the stack trace will tell you where the problem is (ie - line of code)
Still don't understand the GetUder(0); error since I have got a uder with the ID of 0 and it's being used for other tasks in other usercontrols :) But let's just leave it at that.
I found the solution to this. I'm using the code I pasted in the first post, but it seems that the web.config appSettings key somehow returned the wrong value(?) When I set the mediaRootPath directly in the code, it worked right away!
So, for future reference: place the full path to your website directly in the mediaRootPath and not in the web.config :)
Access to path 'nodeId' denied
Hi all and thanks for a great forum!
I just got my hands on some code snippet that I found here on the forum in an old post. The purpose is to create a Media from the path of a FileUpload control and return the media. I would like to credit the guy who made this code, but I simply can't remember his name :/
Anyway, when I run the code it gives me the error "Access to path 'the id of media node' denied" It does make the actual media node in the Media content tree, but there's no data in it.I have placed a key in my web.config under appSettings to point to the media folder of my site: D:/WEB/mysite.dk/www/media, which I believe is the right path (got it from the Server.MapPath("."); method)
Anyone got this problem before and know of a workaround?
The code to create the media item is:
Thanks in advance!
Bo
Tried to make it plain simple like this:
But I get then I get this error:
No node exists with id '0'
Which I'm guessing it means User.GetUser(0)
Never really tried to make a image upload from the frontend, so any hint is welcome ;)
User.GetUser(0) is unlikely to throw an error, if you don't have a user ID of 0 a lot of Umbraco will error, I'd be looking at the MediaType.GetByAlias.
With your initial question I'm sure the stack trace will tell you where the problem is (ie - line of code)
hi slace - thanks for tuning in!
Still don't understand the GetUder(0); error since I have got a uder with the ID of 0 and it's being used for other tasks in other usercontrols :) But let's just leave it at that.
I found the solution to this. I'm using the code I pasted in the first post, but it seems that the web.config appSettings key somehow returned the wrong value(?) When I set the mediaRootPath directly in the code, it worked right away!
So, for future reference: place the full path to your website directly in the mediaRootPath and not in the web.config :)
All the best,
Bo
string mediaRootPath = HttpContext.Current.Server.MapPath("~/media/");
is working on a reply...