I want to save images(photos) in database using two Columns:
1) PhotoMimeType (string)
2) PhotoData (binary)
Have you got any sample codes or examples how to do it in serenity platform.
Thanks
I don't have a sample, but you can write your own behavior, looking at ImageUploadBehavior, replacing file copy operations with saving to database.
Thank you, finally i did it , I found the way to save photo in database
Maybe someone will find it useful:
public class CustomImageUploadBehavior : ImageUploadBehavior
{
public string PhotoMimeTipe { get; set; }
public byte[] PhotoData { get; set; }
public string newFilename { get; set; }
public string path { get; set; }
public string ThumbFilePath { get; set; }
public string TempFolder { get; set; }
public override void OnBeforeSave(ISaveRequestHandler handler)
{
base.OnBeforeSave(handler);
var filename = (StringField)(Target);
newFilename = filename[handler.Row] = filename[handler.Row].TrimToNull();
if (newFilename == null)
return;
path = UploadHelper.DbFilePath(UploadHelper.ToPath(newFilename));
string[] esetse = UploadHelper.GetThumbFileName(newFilename).Split('\\');
TempFolder = UploadHelper.TemporaryPath;
ThumbFilePath = UploadHelper.RootPath + UploadHelper.ToPath(newFilename).Split('\\')[0] + "\\" + esetse[1] + "\\" + esetse[2];
PhotoMimeTipe = UploadHelper.GetMimeType(newFilename);
PhotoData = File.ReadAllBytes(path);
}
public override void OnAfterSave(ISaveRequestHandler handler)
{
base.OnAfterSave(handler);
new SqlUpdate(handler.Row.Table)
.Set("PhotoMimeType", ((newFilename == null) ? Convert.ToString(null) : PhotoMimeTipe))
.Set("PhotoData", ((newFilename == null) ? new byte[0] : PhotoData))
.WhereEqual((Field)((IIdRow)handler.Row).IdField, ((IIdRow)handler.Row).IdField[handler.Row].Value)
.Execute(handler.UnitOfWork.Connection);
File.Delete(path);
File.Delete(ThumbFilePath);
DirectoryInfo di = new DirectoryInfo(TempFolder);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
}
}
And now i have got another problem, how to display photo in client side. I think that i should do do it in Script project, but i have no idea how. Have you got any ideas or suggestions?
Thanks
P.S.
serenity still copies images to folders, and we delete it manually after all
You need to write a MVC action that returns this data. Use that actions URL writing your own ImageUploadEditor. Really this is too much work.
Btw, you can disable default Serenity ImageUploadBehavior, by overriding your repository GetBehaviors method and filtering ImageUplaodBehavior by its type.
So i should Write my own ImageUploadEditorAttribute, but where(folder)?
On script side, something like MyImageUploadEditor, again taking ImageUploadEditor source as sample. I'll think about adding a binary option to ImageUploadEditor though it won't work for multiple images and not sure how to send back images from service to client side.
How is CustomImageUploadBehavior linked to a specific row class? By namespace? Looking for some more information on how to use it. Many thanks in advance.
Most helpful comment
Thank you, finally i did it , I found the way to save photo in database
Maybe someone will find it useful:
And now i have got another problem, how to display photo in client side. I think that i should do do it in Script project, but i have no idea how. Have you got any ideas or suggestions?
Thanks
P.S.
serenity still copies images to folders, and we delete it manually after all