MY CODE :
http://briancaos.wordpress.com/2009/07/09/adding-a-file-to-the-sitecore-media-library-programatically/
본질적으로 기존 문서를 수정하지 않고 새 문서를 업로드한다는 것을 기억하십시오.
다음은 미디어 라이브러리에서 XML 문서를 검색하고 새 노드를 추가 한 다음 다시 같은 위치에 저장하는 샘플 코드입니다.
나는 내가 이미 미디어 라이브러리에서 파일을 가지고있는 파일의 경로를 해달라고 동적에 의해 필요 렸기 때문에 좋은 없습니다
string mediaItemPath = "/sitecore/media library/Files/TestDoc";
string fileName = "TestDoc.xml";
//get the content db, i.e. master db
Sitecore.Data.Database contentDB = Sitecore.Context.ContentDatabase ?? Sitecore.Context.Database;
//get the media file
MediaItem mi = contentDB.GetItem(mediaItemPath);
if (mi == null)
throw new ItemNotFoundException(mediaItemPath);
if (!mi.Extension.Equals("xml") || !mi.MimeType.Equals("text/xml"))
throw new MediaException(string.Format("File {0} was not of correct XML format", mediaItemPath));
//load xml document using media stream
var xDoc = System.Xml.Linq.XDocument.Load(mi.GetMediaStream());
////manipulate the xml as required
////-- update or add new node or whatever
string nodeName = "Item";
string nodeValue = string.Format("{0} {1}", "test node ", DateTime.Now.ToString("yyyyMMddhhmmss"));
xDoc.Element("rootNode")
.Add(new System.Xml.Linq.XElement(nodeName, nodeValue));
//save the modified document into a stream
var xmlStream = new System.IO.MemoryStream();
xDoc.Save(xmlStream);
// Create the options
var options = new Sitecore.Resources.Media.MediaCreatorOptions
{
FileBased = false, // Store the file in the database, not as a file
IncludeExtensionInItemName = false, // Keep file extension in item name
KeepExisting = false, // Keep any existing file with the same name
Versioned = false, // Do not make a versioned template
Destination = mediaItemPath, // set the path
Database = contentDB // Set the database
};
//Use a security disabler to allow changes
using (new Sitecore.SecurityModel.SecurityDisabler())
{
//save the item back into media library
Item mediaItem = Sitecore.Resources.Media.MediaManager.Creator.CreateFromStream(xmlStream, fileName, options);
xmlStream.Dispose();
}
, 당신은 내가 메모리에서 파일을하지 만드는 몇 가지 방법이 필요합니다 aproach folowing from disk – devMd
난 그 제조법을 정확하게 따르는 것을 말하는 것이 아니며 단지 파일을 메모리로 스트리밍하고, 수정 한 다음 미디어 라이브러리에 스트림으로 만들 것을 말하는 것입니다. 'mediaItem = Sitecore.Resources.Media.MediaManager.Creator.CreateFromStream (ms, fileName, options)'는 현재 호출중인 OwnerDOcument.Save 메소드가 아니라 디스크에 저장된 항목에 대해서만 작동합니다. – jammykam
나는 그것을 얻지 못한다. 내가 새로운 스트림에 파일을 연관시키는 방법을 이해할 수 없다. 나는이 je에서 새로운 것이다. – devMd