First of all you need to create a new media type called PDF. You can inherit from the file media type. No extra properties are needed.
After that you need to add a eventhandler that converts your uploaded fileds with a pdf extension from the File media type to the PDF media type.
This should to do the trick.
namespace YourNameSpace
{
/// <summary>
/// Overriden ApplicationEventHandler class
/// </summary>
public class RegisterMediaEvents : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
MediaService.Saved += this.MediaService_Saved;
}
private void MediaService_Saved(IMediaService sender, SaveEventArgs<IMedia> e)
{
foreach (var item in e.SavedEntities)
{
// only do this for file uploads
if (item.ContentType.Alias.Equals(Models.ContentModels.File.ModelTypeAlias))
{
var filePropertyValue = item.GetValue("umbracoFile");
if (filePropertyValue != null && !string.IsNullOrEmpty(filePropertyValue.ToString()))
{
var filename = filePropertyValue.ToString();
// check if the uploaded file is a pdf
var isPdf = filename.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase)
if (isPdf)
{
var contentTypeService = Umbraco.Core.ApplicationContext.Current.Services.ContentTypeService;
var pdfMediaType = contentTypeService.GetMediaType("pdfMediaTypeAlias");
if (pdfMediaType != null)
{
item.ChangeContentType(pdfMediaType, true);
Umbraco.Core.ApplicationContext.Current.Services.MediaService.Save(item, 0, false);
}
}
}
}
}
}
}
}
it's a little bit strange, but really you can't get the mediaType with MediaService. You can only get the mediaType with contentTypeService. In umbraco database contentTypes and mediaTypes are in the same table.
File Upload - choose a mediatype depending on extension
If I upload (drag'n'drop) a jpg/png/gif file the media-node created is of type "Image". All others are of type "File".
Is it possible to add file-extension so fx ".pdf" is going to be of media-type "PDF".
-Mikkel
I have just looked in the Umbraco source code in the MediaController.
Looks like that the core is default setting the mediatype to "File" or if the file-extension is jpg/png/ext. it is a "Image".
So no option of doing any thing like file-extion => mediatype in any config-files.
Hi Mikkel,
First of all you need to create a new media type called PDF. You can inherit from the file media type. No extra properties are needed.
After that you need to add a eventhandler that converts your uploaded fileds with a pdf extension from the File media type to the PDF media type.
This should to do the trick.
Hi Dave,
very great! I've been looking so long for such a solution. #h5yr
Cheers,
Sören
Thanks Dave :-)
I was looking for a non-event soloution. Looks like I must be the event-way.
By the way shouldn't be the MediaService your are using?
var contentTypeService = Umbraco.Core.ApplicationContext.Current.Services.ContentTypeService;
Hi Mikkel,
it's a little bit strange, but really you can't get the mediaType with MediaService. You can only get the mediaType with contentTypeService. In umbraco database contentTypes and mediaTypes are in the same table.
Cheers,
Sören
That's used to get the media type. A media type is also a content type but a special one.
Dave
Also if it works, can you mark this post as a solution, so others can find it as well.
dave
is working on a reply...