I've got the following situation. After uploading an image with uploadify I want to get the image and resize it in different formats. I thought I could use the Media.New event for this, but if this event is called no image is set yet (umbracoFile is empty). This is probably done after the media file is created. Is there some way (like a different event) I can still get the image to resize it?
What version of umbraco? I tried AfterSave with 4.0.3 and still didnt get a file so i had to do:
mediaItem m = getMediaData(id);
//update the doc xml
node = addMediaInfoToXmlDoc(m, node);
/// <summary>
/// add the missing info to xml doc so that we can index the media item
/// </summary>
/// <param name="m"></param>
/// <param name="n"></param>
/// <returns></returns>
private XmlNode addMediaInfoToXmlDoc(mediaItem m, XmlNode n){
XmlNode node = n;
n.SelectSingleNode("/node/data[@alias='umbracoFile']").InnerText = m.url;
n.SelectSingleNode("/node/data[@alias='umbracoExtension']").InnerText = m.ext;
return node;
}
/// <summary>
/// have to do this becuase we dont have it in the xml
/// something todo with order of events, ie item is saved but file path and extension is not picked up at this point
/// </summary>
/// <param name="mediaId"></param>
/// <returns></returns>
private mediaItem getMediaData(int mediaId) {
string sql = "select dataNvarchar from cmsPropertyData where contentnodeid=@id and (propertytypeid=24 or propertytypeid=25)";
mediaItem md = new mediaItem();
StringBuilder sb=new StringBuilder();
IRecordsReader dr = SqlHelper.ExecuteReader(sql, SqlHelper.CreateParameter("@id", mediaId));
using (dr)
{
while (dr.Read())
{
sb.Append(dr.GetString("dataNvarchar"));
sb.Append(",");
}
}
string result = sb.ToString().TrimEnd(new char[] { ',' });
string[] res = result.Split(new char[] { ',' });
md.url = res[0];
md.ext = res[1];
return md;
}
#endregion
public static ISqlHelper SqlHelper
{
get
{
return Application.SqlHelper;
}
}
There is in fact an event you can subscribe to in order to make any further processing on the image. The Uploadify class has a static FileUploaded event that you can use. The FileUploadedEventArgs argument to the subscribed methods will contain an UploadifySettings object with the following properties:
int MediaFolderId - the destination folder for the file User UploaderUser - the user uploading the file, if exists Member UploaderMember - the member uploading the file from the front-end, if exists HttpPostedFile File - the file bool Cancelled - if you set this flag to TRUE, the upload event will be cancelled string CancelledMessage - if the event is cancelled, this message will be displayed to the user
Let me know if you need further help, or if something doesn't work.
Media events
Hello,
I've got the following situation. After uploading an image with uploadify I want to get the image and resize it in different formats. I thought I could use the Media.New event for this, but if this event is called no image is set yet (umbracoFile is empty). This is probably done after the media file is created. Is there some way (like a different event) I can still get the image to resize it?
Jeroen
Ok already got it. The Media.AfterSave also get's called and in this event the image is available :).
Jeroen,
What version of umbraco? I tried AfterSave with 4.0.3 and still didnt get a file so i had to do:
Hi Jeroen,
There is in fact an event you can subscribe to in order to make any further processing on the image.
The Uploadify class has a static FileUploaded event that you can use.
The FileUploadedEventArgs argument to the subscribed methods will contain an UploadifySettings object with the following properties:
int MediaFolderId - the destination folder for the file
User UploaderUser - the user uploading the file, if exists
Member UploaderMember - the member uploading the file from the front-end, if exists
HttpPostedFile File - the file
bool Cancelled - if you set this flag to TRUE, the upload event will be cancelled
string CancelledMessage - if the event is cancelled, this message will be displayed to the user
Let me know if you need further help, or if something doesn't work.
Cheers,
Emanuel
@Ismail I'm Using Umbraco 4.5 so that must be difference.
@Emanuel Thanks for the info. Since the Media.AfterSave works for me I'll just use that.
Jeroen
is working on a reply...