ASPX templates
Version 5.x > ASPX templates > Deleting media library image from codebehind View modes: 
User avatar
Member
Member
ovidiu-e-cubed - 11/28/2011 9:49:39 AM
   
Deleting media library image from codebehind
Hi,

I'm trying to delete an image from the media library using codebehind. I'm trying to overwrite that image so I'm trying to delete it first. But I'm getting an error:

The object with code name '87556833-1f14-4aef-b370-ba955c2f023a' already exists.

I used this code:

MediaFileInfoProvider.DeleteMediaFile(CMSContext.CurrentSiteID, libraryInfo.LibraryID, path, false);

// delete file if it exists
if (File.Exists(path))
{
File.Delete(path);
}

using (var newFile = new FileStream(path, FileMode.Create))
{
newFile.Write(buffer, 0, buffer.Length);
}

//create media file info item
var fileInfo = new MediaFileInfo(path, libraryInfo.LibraryID, "NCDA")
{
FileTitle = fileName,
FileDescription = "",
};

// Save media file info
try
{
MediaFileInfoProvider.ImportMediaFileInfo(fileInfo);
}
catch { }


I checked the database and no table seems to actually have this GUID stored anywhere. Really strage.

Is there any way that I can replace an image from the media library using asp.net code?

Thanks.

User avatar
Member
Member
ovidiu-e-cubed - 11/28/2011 10:01:14 AM
   
RE:Deleting media library image from codebehind
I also tried setting the synchronization flag to true, but to no avail:

MediaFileInfoProvider.DeleteMediaFile(CMSContext.CurrentSiteID, libraryInfo.LibraryID, path, true);

User avatar
Member
Member
kentico_michal - 11/29/2011 6:48:05 AM
   
RE:Deleting media library image from codebehind
Hello,

Please make sure that the CMS_MetaFile and the Media_File table does not contain a record with this GUID.

Anyway, you do not need to delete the original file and create a new one. You can just update the MediaFileInfo and use MediaFileInfoProvider.SetMediaFileInfo to update it.

Best regards,
Michal Legen

User avatar
Member
Member
ovidiu-e-cubed - 12/2/2011 10:08:09 AM
   
RE:Deleting media library image from codebehind
Hi,

We have tried your suggestion but we're getting the same error. An object with some guid already exists.

This is the method that uploads a banner and fails to overwrite in media library if it's already there:

 private void UploadBanner(string mediaLibFolder, HtmlInputFile input)
{
var fileName = dealershipId.ToString() + ".jpg";
string fName = dealershipId.ToString();
var streamLength = input.PostedFile.ContentLength;
var z = input.PostedFile.ContentType;
var inputStream = input.PostedFile.InputStream;
var br = new BinaryReader(inputStream);
var buffer = br.ReadBytes((Int32)inputStream.Length);
var website = CMSContext.CurrentSiteName;

//Media Library Info - takes Media Library Name and Website Name
var libraryInfo = MediaLibraryInfoProvider.GetMediaLibraryInfo("NCDA", website);
if (libraryInfo == null)
{
ImageUploadErrorDiv.Style["display"] = "block";
return;
}

//Absolute Path to File
string filePath = Server.MapPath("~/NCDA/media/NCDA/" + mediaLibFolder + "/" + fileName);
//Get Relative Path to File
string path = MediaLibraryHelper.EnsurePath(filePath);
string path_thumbnails = Server.MapPath("~/NCDA/media/NCDA/" + mediaLibFolder + "/__thumbnails");
string[] fileListAll = System.IO.Directory.GetFiles(path_thumbnails);

var fileList = System.IO.Directory.GetFiles(path_thumbnails, fName + "*");
foreach (string file in fileList)
{
System.IO.File.Delete(file);
}

try
{
using (var newFile = new FileStream(path, FileMode.Create, FileAccess.Write))
{
newFile.Write(buffer, 0, buffer.Length);
newFile.Flush();
newFile.Close();
}

//create media file info item
var fileInfo = new MediaFileInfo(path, libraryInfo.LibraryID, "NCDA")
{
FileTitle = fileName,
FileDescription = "",
};

// Save media file info
try
{
MediaFileInfoProvider.DeleteMediaFileThumbnails(fileInfo);
MediaFileInfoProvider.DeleteMediaFilePreview(website, libraryInfo.LibraryID, path, false);
MediaFileInfoProvider.SetMediaFileInfo(fileInfo);
}
catch { }

BannerUploadedDiv.Style["display"] = "block";
}
catch
{
ImageUploadErrorDiv.Style["display"] = "block";
}
}

User avatar
Member
Member
kentico_michal - 12/12/2011 1:57:29 AM
   
RE:Deleting media library image from codebehind
Hello,

Just a short update regarding this forum thread. This issue was solved via standard support (support@kentico.com).

Best regards,
Michal Legen