本文整理汇总了C#中umbraco.cms.businesslogic.media.Media.Save方法的典型用法代码示例。如果您正苦于以下问题:C# Media.Save方法的具体用法?C# Media.Save怎么用?C# Media.Save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类umbraco.cms.businesslogic.media.Media
的用法示例。
在下文中一共展示了Media.Save方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DoHandleMedia
public override void DoHandleMedia(Media media, PostedMediaFile uploadedFile, User user)
{
// Get umbracoFile property
var propertyId = media.getProperty("umbracoFile").Id;
// Get paths
var destFilePath = FileSystem.GetRelativePath(propertyId, uploadedFile.FileName);
var ext = Path.GetExtension(destFilePath).Substring(1);
//var absoluteDestPath = HttpContext.Current.Server.MapPath(destPath);
//var absoluteDestFilePath = HttpContext.Current.Server.MapPath(destFilePath);
// Set media properties
media.getProperty("umbracoFile").Value = FileSystem.GetUrl(destFilePath);
media.getProperty("umbracoBytes").Value = uploadedFile.ContentLength;
if (media.getProperty("umbracoExtension") != null)
media.getProperty("umbracoExtension").Value = ext;
if (media.getProperty("umbracoExtensio") != null)
media.getProperty("umbracoExtensio").Value = ext;
FileSystem.AddFile(destFilePath, uploadedFile.InputStream, uploadedFile.ReplaceExisting);
// Save media
media.Save();
}
示例2: DoHandleMedia
public override void DoHandleMedia(Media media, PostedMediaFile uploadedFile, User user)
{
// Get umbracoFile property
var propertyId = media.getProperty(Constants.Conventions.Media.File).Id;
// Get paths
var destFilePath = FileSystem.GetRelativePath(propertyId, uploadedFile.FileName);
var ext = Path.GetExtension(destFilePath).Substring(1);
//var absoluteDestPath = HttpContext.Current.Server.MapPath(destPath);
//var absoluteDestFilePath = HttpContext.Current.Server.MapPath(destFilePath);
// Set media properties
media.getProperty(Constants.Conventions.Media.File).Value = FileSystem.GetUrl(destFilePath);
media.getProperty(Constants.Conventions.Media.Bytes).Value = uploadedFile.ContentLength;
if (media.getProperty(Constants.Conventions.Media.Extension) != null)
media.getProperty(Constants.Conventions.Media.Extension).Value = ext;
// Legacy: The 'extensio' typo applied to MySQL (bug in install script, prior to v4.6.x)
if (media.getProperty("umbracoExtensio") != null)
media.getProperty("umbracoExtensio").Value = ext;
FileSystem.AddFile(destFilePath, uploadedFile.InputStream, uploadedFile.ReplaceExisting);
// Save media
media.Save();
}
示例3: update
public void update(mediaCarrier carrier, string username, string password)
{
Authenticate(username, password);
if (carrier == null) throw new Exception("No carrier specified");
Media m = new Media(carrier.Id);
if (carrier.MediaProperties != null)
{
foreach (mediaProperty updatedproperty in carrier.MediaProperties)
{
if (!(updatedproperty.Key.ToLower().Equals("umbracofile")))
{
Property property = m.getProperty(updatedproperty.Key);
if (property == null)
throw new Exception("property " + updatedproperty.Key + " was not found");
property.Value = updatedproperty.PropertyValue;
}
}
}
m.Save();
}
示例4: DoHandleMedia
public override void DoHandleMedia(Media media, PostedMediaFile postedFile, User user)
{
// Set media property to upload the file as well as set all related properties
media.MediaItem.SetValue("umbracoFile", postedFile.FileName, postedFile.InputStream);
// Copy back the values from the internal IMedia to ensure that the values are persisted when saved
foreach (var property in media.MediaItem.Properties)
{
media.getProperty(property.Alias).Value = property.Value;
}
// Save media (using legacy media object to ensure the usage of the legacy events).
media.Save();
}
示例5: writeContents
public void writeContents(int id, string filename, Byte[] contents, string username, string password)
{
Authenticate(username, password);
filename = filename.Replace("/", global::Umbraco.Core.IO.IOHelper.DirSepChar.ToString());
filename = filename.Replace(@"\", global::Umbraco.Core.IO.IOHelper.DirSepChar.ToString());
filename = filename.Substring(filename.LastIndexOf(global::Umbraco.Core.IO.IOHelper.DirSepChar) + 1, filename.Length - filename.LastIndexOf(global::Umbraco.Core.IO.IOHelper.DirSepChar) - 1).ToLower();
var m = new Media(id);
var numberedFolder = MediaSubfolderCounter.Current.Increment();
var path = _fs.GetRelativePath(numberedFolder.ToString(CultureInfo.InvariantCulture), filename);
var stream = new MemoryStream();
stream.Write(contents, 0, contents.Length);
stream.Seek(0, 0);
_fs.AddFile(path, stream);
m.getProperty("umbracoFile").Value = _fs.GetUrl(path);
m.getProperty("umbracoExtension").Value = Path.GetExtension(filename).Substring(1);
m.getProperty("umbracoBytes").Value = _fs.GetSize(path);
m.Save();
}
示例6: DoHandleMedia
public override void DoHandleMedia(Media media, PostedMediaFile postedFile, BusinessLogic.User user)
{
// Get Image object, width and height
var image = System.Drawing.Image.FromStream(postedFile.InputStream);
var fileWidth = image.Width;
var fileHeight = image.Height;
// Get umbracoFile property
var propertyId = media.getProperty("umbracoFile").Id;
// Get paths
var destFileName = ConstructDestFileName(propertyId, postedFile.FileName);
var destPath = ConstructDestPath(propertyId);
var destFilePath = VirtualPathUtility.Combine(destPath, destFileName);
var ext = VirtualPathUtility.GetExtension(destFileName).Substring(1);
var absoluteDestPath = HttpContext.Current.Server.MapPath(destPath);
var absoluteDestFilePath = HttpContext.Current.Server.MapPath(destFilePath);
// Set media properties
media.getProperty("umbracoFile").Value = destFilePath;
media.getProperty("umbracoWidth").Value = fileWidth;
media.getProperty("umbracoHeight").Value = fileHeight;
media.getProperty("umbracoBytes").Value = postedFile.ContentLength;
if (media.getProperty("umbracoExtension") != null)
media.getProperty("umbracoExtension").Value = ext;
if (media.getProperty("umbracoExtensio") != null)
media.getProperty("umbracoExtensio").Value = ext;
// Create directory
if (UmbracoSettings.UploadAllowDirectories)
Directory.CreateDirectory(absoluteDestPath);
// Generate thumbnail
var thumbDestFilePath = Path.Combine(absoluteDestPath, Path.GetFileNameWithoutExtension(destFileName) + "_thumb");
GenerateThumbnail(image, 100, fileWidth, fileHeight, thumbDestFilePath + ".jpg");
// Generate additional thumbnails based on PreValues set in DataTypeDefinition uploadField
GenerateAdditionalThumbnails(image, fileWidth, fileHeight, thumbDestFilePath);
image.Dispose();
// Save file
postedFile.SaveAs(absoluteDestFilePath);
// Close stream
postedFile.InputStream.Close();
// Save media
media.Save();
}
示例7: Save
/// <summary>
/// Save the kraked image to a specific Umbraco Media node
/// </summary>
/// <param name="imKrakTarget">Target media node</param>
/// <param name="keepOriginal">Save the original image in Umbraco? Pass NULL to use global settings</param>
/// <param name="hasChanged">Has a new image been selected for the media node just now?</param>
/// <returns>Success status</returns>
internal bool Save(Media mKrakTarget, bool? keepOriginal = null, bool hasChanged = false)
{
umbraco.cms.businesslogic.property.Property p;
// Validate parameters
var status = GetKrakStatus(mKrakTarget);
if (status == EnmIsKrakable.Unkrakable || status == EnmIsKrakable.Original || String.IsNullOrEmpty(kraked_url))
// This image is unkrakable, do not proceed
return false;
// Determine the path and the name of the image
var relativeFilepath = mKrakTarget.getProperty(Constants.UmbracoPropertyAliasFile).Value.ToString();
var relativeDirectory = System.IO.Path.GetDirectoryName(relativeFilepath);
var absoluteDirectory = System.Web.Hosting.HostingEnvironment.MapPath("~" + relativeDirectory);
string filename = Path.GetFileName(relativeFilepath);
if (keepOriginal == null)
keepOriginal = Configuration.Settings.KeepOriginal;
// Has this media node already been Kraked before?
int originalSize = 0;
if (status == EnmIsKrakable.Kraked)
{
p = mKrakTarget.getProperty(Constants.UmbracoPropertyAliasOriginalSize);
if (p != null && p.Value != null)
int.TryParse(p.Value.ToString(), out originalSize);
}
if (originalSize == 0)
originalSize = original_size;
var compressionRate = (((decimal)(originalSize - kraked_size)) / originalSize).ToString("p2");
// The following might seem redundant, but Umbraco's "SetValue" extension method used below actually does a lot of magic for us.
// However, Umbraco will also create a new media folder for us to contain the new image which we do NOT want (the url to the image has to remain unchanged).
// So some extra efforts are required to make sure the compressed image will be switched in the place of the original image.
var originalUmbracoFilePropertyData = mKrakTarget.getProperty(Constants.UmbracoPropertyAliasFile).Value.ToString(); // Get the original property data
if (!mKrakTarget.AddFile(kraked_url, filename))
return false; // Krak failed
// Extract the absolute directory path
var newRelativeFilepath = mKrakTarget.getProperty(Constants.UmbracoPropertyAliasFile).Value.ToString(); // Retrieve the relative filepath to the new image location
var newRelativeDirectory = System.IO.Path.GetDirectoryName(newRelativeFilepath); // Extract the relative directoryname
var newAbsoluteDirectory = System.Web.Hosting.HostingEnvironment.MapPath("~" + newRelativeDirectory); // Convert to it's absolute variant
mKrakTarget.getProperty(Constants.UmbracoPropertyAliasFile).Value = originalUmbracoFilePropertyData; // Put the original property data back in place
// If an "original" media node is already present under the current node, then save our original data to that node.
// Else we will keep creating new nodes under the current node each time we save, and we never want more then 1 original node!
var mOriginal = mKrakTarget.Children.FirstOrDefault(x => x.Text == EnmKrakStatus.Original.ToString() && x.getProperty(Constants.UmbracoPropertyAliasStatus) != null && x.getProperty(Constants.UmbracoPropertyAliasStatus).Value as String == "Original");
// Does the original media node already exist?
bool originalExists = mOriginal != null;
// Do we need to keep a backup of the originally kraked image?
if (keepOriginal.Value)
{
if (!originalExists)
// No. Simply create a new "Original" media node under the current node, which will be used to store our "backup"
mOriginal = Media.MakeNew(EnmKrakStatus.Original.ToString(), MediaType.GetByAlias(mKrakTarget.ContentType.Alias), mKrakTarget.User, mKrakTarget.Id);
// We are only allowed to MODIFY the ORIGINAL media node if the FILE has CHANGED! If the original file has not been modified, then we are ONLY allowed to create a NEW media node (aka it didn't exist before)
if (hasChanged || !originalExists)
{
// Copy all properties of the current media node to the original (aka: BACKUP)
foreach (var p2 in mOriginal.GenericProperties)
p2.Value = mKrakTarget.getProperty(p2.PropertyType.Alias).Value;
// The image has been modified during the saving proces before, so correct that by specifying the correct original imag
p = mOriginal.getProperty(Constants.UmbracoPropertyAliasFile);
if (p != null)
// Save the original data, but replace the old relative filepath with the new one
p.Value = originalUmbracoFilePropertyData.Replace(relativeFilepath, newRelativeFilepath);
// The same for filesize
p = mOriginal.getProperty(Constants.UmbracoPropertyAliasSize);
if (p != null)
p.Value = originalSize;
// Set the "status" of the original image to "Original", so we know in the future this is the original image
p = mOriginal.getProperty(Constants.UmbracoPropertyAliasStatus);
if (p != null)
p.Value = EnmKrakStatus.Original.ToString();
// Save the original node. It will be placed directly underneath the current media node
mOriginal.Save();
// Now swap the folders so everything is correct again
string tmpFolder = absoluteDirectory + "_tmp";
System.IO.Directory.Move(absoluteDirectory, tmpFolder);
System.IO.Directory.Move(newAbsoluteDirectory, absoluteDirectory);
System.IO.Directory.Move(tmpFolder, newAbsoluteDirectory);
}
else
{
// Leave the original alone! So just replace the target folder with the compressed version
if (System.IO.Directory.Exists(absoluteDirectory))
//.........这里部分代码省略.........