tôi cũng sẽ chọn để sử dụng một số nhị phân để đạt được điều này, nhưng nếu bạn muốn quản lý tệp htaccess bằng văn bản, chứ không phải là thành phần đa phương tiện, bạn có thể đẩy nhị phân vào gói của mình bằng cách sử dụng kỹ thuật sau:
1) Đẩy văn bản của tệp Htaccess vào gói có tên có thể truy cập (tức là Binary_Text) 2) Sử dụng mã tương tự như sau để tạo tệp văn bản từ văn bản trong biến và thêm nó vào gói
class publishStringItemAsBinary : ITemplate
{
public void Transform(Engine engine, Package package)
{
TemplatingLogger log = TemplatingLogger.GetLogger(typeof(publishStringItemAsBinary));
TemplateUtilities utils = new TemplateUtilities();
System.IO.Stream inputStream = null;
try
{
string strInputName = package.GetValue("InputItem");
string strFileName = package.GetValue("strFileName");
string sg_Destination = package.GetValue("sg_Destination");
string itemComponent = package.GetValue("mm_Component");
inputStream = new MemoryStream(Encoding.UTF8.GetBytes(package.GetValue(strInputName)));
log.Debug("InputObject:" + strInputName);
log.Debug("Filename for binary:" + strFileName);
log.Debug("Destination StructureGroup:" + sg_Destination);
Publication contextPub = utils.getPublicationFromContext(package, engine);
TcmUri uriLocalSG = TemplateUtilities.getLocalUri(new TcmUri(contextPub.Id), new TcmUri(sg_Destination));
TcmUri uriLocalMMComp = TemplateUtilities.getLocalUri(new TcmUri(contextPub.Id), new TcmUri(itemComponent));
StructureGroup sg = (StructureGroup)engine.GetObject(uriLocalSG);
Component comp = (Component)engine.GetObject(uriLocalMMComp);
String sBinaryPath = engine.PublishingContext.RenderedItem.AddBinary(inputStream, strFileName, sg, "nav", comp, "text/xml").Url;
//Put a copy of the path in the package in case you need it
package.PushItem("BinaryPath", package.CreateStringItem(ContentType.Html, sBinaryPath));
}
catch (Exception e)
{
log.Error(e.Message);
}
finally
{
if (inputStream != null)
{
inputStream.Close();
}
}
}
}
Tôi nghĩ mã này khá tự giải thích. Điều này xuất bản một nhị phân loại văn bản/xml, nhưng không có vấn đề chuyển đổi nó để làm một tập tin văn bản đơn giản.
Nguồn
2012-04-13 11:41:23
Chris, tôi đã quyết định biến nó thành công việc triển khai ngay bây giờ nhưng mã này trông thực sự tốt cho một lần khác. Cảm ơn bạn. –