API Questions on Kentico API.
Version 5.x > API > Upload images to media lib View modes: 
User avatar
Member
Member
mike.shaker-live - 11/28/2012 7:48:47 AM
   
Upload images to media lib
Hi,
I am new to kentico .
I made new Module with vb.net I am trying to upload image to the lib and show preview of it before send the email.
i can not get it to upload the image. so if anyone know easy way to upload it. or how can i add web part to mycode to let me upload it to the media lib.
Thanks

my code is like that
Dim CouponfileName As String = ""

Dim savePath As String = "~/AMR/media/AMR/"
'Dim savePath As String = "\\djjgxfs\AlmanacDocuments\publicfiles\"
If (fileImage.HasFile) Then
Dim imagename As String = Server.HtmlEncode(fileImage.FileName)
savePath += fileImage.FileName
CouponfileName = fileImage.FileName
fileImage.SaveAs(savePath)

lblStatus.Text = "Your file was uploaded successfully"
Else
lblStatus.Text = "NO IMG"
End If

User image

User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 11/30/2012 7:46:43 PM
   
RE:Upload images to media lib
Hello,

If you are talking about media libraries, then please find some example code below:

private bool CreateMediaFile()
{
// Prepare the parameters
string filePath = "~/CMSAPIExamples/Code/Tools/MediaLibrary/Files/Powered_by_kentico2.gif";

// Get media library
MediaLibraryInfo library = MediaLibraryInfoProvider.GetMediaLibraryInfo("MyNewLibrary", CMSContext.CurrentSiteName);
if (library != null)
{
// Create new media file object
MediaFileInfo mediaFile = new MediaFileInfo(Server.MapPath(filePath), library.LibraryID);

// Create file info
FileInfo file = FileInfo.New(Server.MapPath(filePath));
if (file != null)
{
// Set the properties
mediaFile.FileName = "MyNewFile";
mediaFile.FileTitle = "My new file title";
mediaFile.FileDescription = "My new file description.";
mediaFile.FilePath = "MyNewFolder/MyNewFile.gif";
mediaFile.FileExtension = file.Extension;
mediaFile.FileMimeType = "image/gif";
mediaFile.FileSiteID = CMSContext.CurrentSiteID;
mediaFile.FileLibraryID = library.LibraryID;
mediaFile.FileSize = file.Length;

// Create the media file
MediaFileInfoProvider.SetMediaFileInfo(mediaFile);

return true;
}
}

return false;
}


Please note, that there are additional code examples in our CMSAPIExamples section available under <your domain name>/CMSAPIExamples:

User image

Best regards,
Boris Pocatko