I've managed successfully to create a new media item using the media API but I have a quiestion about the media file structure on the file system
All content seems to be in one folder per node like /media/NNN/ where NNN is some id. When I create with the API I use the node ID, but just for comparison I created media using the umbraco interface, the folder number was a 3 digit number and not the node id.
Was I wrong? what is this NNN number that the umbraco screens use for media ID
Umbraco is using the property Id instead of the media Id. Which makes sense because media is stored using the upload datatype and that datatype can be used in Documents also. In the example below I have the media object and get the id from the property and use that id to store the file in the media folder
Thanks for answering - That seems a little strange to me though becuase when I create the media, I need to assign the property umbracoFile to contain the path to the file - and if the path contains the ID of that property, I will need to create the property without knowing the path, then assign it and change it to include the ID. I can probably explain with a snippet below
Media newMedia; newMedia = Media.MakeNew(filename, new MediaType(mediaType), admUser, folderNodeId);
string newName = filename; // This is calcualted earlier when file is uploaded string newFolder = context.Server.MapPath("/media") + "\\" + newMedia.Id; string newFile = newFolder + "\\" + newName;
// This is where I copy the file to the final location. At this stage I've not created the umbracoFile property as I need to know the path before hand
Directory.CreateDirectory(newFolder);
File.Move(savepath + "\\" + filename, newFile);
// Save filename and path to Umrbaco File newMedia.getProperty("umbracoFile").Value = "/media/" + newMedia.Id + "/" + newName;
// Save type newMedia.getProperty("umbracoExtension").Value = extension.Replace(".", "");
// Save size FileInfo f = new FileInfo(newFile); newMedia.getProperty("umbracoBytes").Value = f.Length;
// Generate Thumbnail for images - isImage is a bool set earlier in the processing, worked out based on extension if (isImage) {
// Resize the image here to create the thumbnails and calculate image height
Hope that makes sense. Essentially I can't find the property ID before I know the path, but the path contains the ID
I can probably do something along the lines of create the property with a dummy path, find the ID and then move it again to the correct path with the final ID but that seems counter productive
public bool UploadFile()
{
bool subio = false;
if (fuAttachment.HasFile)
{
if (fuAttachment.PostedFile.ContentLength <= 10485760)
{
if (ExtencionPermitida())
{
if (!Directory.Exists(@"C:\\Inetpub\\wwwroot\\catarsyslab.com\\Umbraco\\TicketAttachments\\"))
{
try
{ Directory.CreateDirectory(@"C:\\Inetpub\\wwwroot\\catarsyslab.com\\Umbraco\\TicketAttachments\\"); }
catch
{ lblMensaje.Text = "Error al subir archivo:<br/><br/> No se puede crear la carpeta."; }
}
fuAttachment.SaveAs(@"C:\\Inetpub\\wwwroot\\catarsyslab.com\\Umbraco\\TicketAttachments\\" + fuAttachment.FileName);
subio = true;
}
else
{
lblMensaje.Text = "El tipo de archivo que desea adjuntar no es permitido."
+ "<br/>Archivos Comprimidos: (.zip)(.rar)"
+ "<br/>Documentos: (.doc)(.xsl)(.pdf)(.ppt)"
+ "<br/>Imagenes: (.jpg)(.gif)(.png)";
}
}
else
{
lblMensaje.Text = "Error al subir archivo:<br/> Archivo muy grande, seleccione otro o comprimalo (.zip)(.rar)";
}
}
else
{
subio = true;
}
return subio;
}
public bool ExtencionPermitida()
{
bool Permitido = false;
String Extencion = fuAttachment.FileName.Substring(fuAttachment.FileName.LastIndexOf(".") + 1).ToUpper();
switch (Extencion)
{
case "ZIP":
case "RAR":
case "DOC":
case "XLS":
case "PDF":
case "PPT":
case "JPG":
case "GIF":
case "PNG":
Permitido = true;
break;
default:
Permitido = false;
break;
}
return Permitido;
}
Create new media using API
Hi All
I've managed successfully to create a new media item using the media API but I have a quiestion about the media file structure on the file system
All content seems to be in one folder per node like /media/NNN/ where NNN is some id. When I create with the API I use the node ID, but just for comparison I created media using the umbraco interface, the folder number was a 3 digit number and not the node id.
Was I wrong? what is this NNN number that the umbraco screens use for media ID
thanks,
Hi Carl
Could you please share some of the code you're using to achieve this? Then it might be much more easy to help you out :-)
/Jan
Hi Carl,
Umbraco is using the property Id instead of the media Id. Which makes sense because media is stored using the upload datatype and that datatype can be used in Documents also. In the example below I have the media object and get the id from the property and use that id to store the file in the media folder
media.getProperty(
"umbracoFile").Id
Hope this helps you,
Richard
Hi Richard
Thanks for answering - That seems a little strange to me though becuase when I create the media, I need to assign the property umbracoFile to contain the path to the file - and if the path contains the ID of that property, I will need to create the property without knowing the path, then assign it and change it to include the ID. I can probably explain with a snippet below
Media newMedia;
newMedia = Media.MakeNew(filename, new MediaType(mediaType), admUser, folderNodeId);
string newName = filename; // This is calcualted earlier when file is uploaded
string newFolder = context.Server.MapPath("/media") + "\\" + newMedia.Id;
string newFile = newFolder + "\\" + newName;
// This is where I copy the file to the final location. At this stage I've not created the umbracoFile property as I need to know the path before hand
Directory.CreateDirectory(newFolder);
File.Move(savepath + "\\" + filename, newFile);
// Save filename and path to Umrbaco File
newMedia.getProperty("umbracoFile").Value = "/media/" + newMedia.Id + "/" + newName;
// Save type
newMedia.getProperty("umbracoExtension").Value = extension.Replace(".", "");
// Save size
FileInfo f = new FileInfo(newFile);
newMedia.getProperty("umbracoBytes").Value = f.Length;
// Generate Thumbnail for images - isImage is a bool set earlier in the processing, worked out based on extension
if (isImage)
{
// Resize the image here to create the thumbnails and calculate image height
newMedia.getProperty("umbracoWidth").Value = image.Width.ToString();
newMedia.getProperty("umbracoHeight").Value = image.Height.ToString();
// Generate thumbnails
string fileNameThumb = context.Server.MapPath("/media/") + newMedia.Id + "/" + newName.Replace(extension, "_thumb");
}
Hope that makes sense. Essentially I can't find the property ID before I know the path, but the path contains the ID
I can probably do something along the lines of create the property with a dummy path, find the ID and then move it again to the correct path with the final ID but that seems counter productive
thanks for your help
Hi,
The property is already available when you create the media item. Only the value isn't assigned.
Try newMedia.getProperty("umbracoFile").Id before you assign the filename and you will get the id of the property.
Cheers,
Richard
Hey Richard
Thanks. I'll give it a go and let you know the outcome
Hi Richard
That worked a treat - thanks for the info
can anyone give me sample code to integrate umbraco api for uploading content items
Thanks
Pramod
Hi Pramod,
Here is a little code that we are using to upload attachments to some node created at the fly. Hope it helps
Thanks Julio,
i need add contents using api
Hi,
If you want to import content items you might want to checkout CmsImport, no need to program.
Cheers,
Richard
This thread just saved me from early dementia, thankyou Richard!
is working on a reply...