Javascript 是否有一种方法可以在移动中使用CKEditor修改MS Word文档并将其保存在服务器中的某个位置
我的要求是建立一个自定义文档库,在这里我可以使用不同的版本保存文档 是否有一种方法可以在移动中使用CKEditor修改MS Word文档(.doc或.docx),并将其保存在服务器中的某个位置Javascript 是否有一种方法可以在移动中使用CKEditor修改MS Word文档并将其保存在服务器中的某个位置,javascript,c#,ms-word,ckeditor,Javascript,C#,Ms Word,Ckeditor,我的要求是建立一个自定义文档库,在这里我可以使用不同的版本保存文档 是否有一种方法可以在移动中使用CKEditor修改MS Word文档(.doc或.docx),并将其保存在服务器中的某个位置 关于任何其他选项的建议也是可以接受的。我不确定.doc是否正确,但对于.docx,您可以通过以下方式将其转换为html 您应该为此实现OpenXMLDLL。 您需要的DLL是 1。DocumentFormat.OpenXml.dll 2。OpenXmlPowerTools.dll 3。System.IO.
关于任何其他选项的建议也是可以接受的。我不确定.doc是否正确,但对于.docx,您可以通过以下方式将其转换为html 您应该为此实现OpenXMLDLL。 您需要的DLL是 1。DocumentFormat.OpenXml.dll 2。OpenXmlPowerTools.dll 3。System.IO.Packaging.dll 以下是代码片段的工作版本。 DocxToHtml.cs类
using System.IO;
using System.Linq;
using OpenXmlPowerTools;
using DocumentFormat.OpenXml.Packaging;
using System.Drawing.Imaging;
using System.Xml.Linq;
public class DocxToHtml
{
public string ConvertToHtml(string fullFilePath)
{
if (string.IsNullOrEmpty(fullFilePath) || Path.GetExtension(fullFilePath) != ".docx")
return "Unsupported format";
FileInfo fileInfo = new FileInfo(fullFilePath);
string htmlText = string.Empty;
try
{
htmlText = ParseDOCX(fileInfo);
}
catch (OpenXmlPackageException e)
{
if (e.ToString().Contains("Invalid Hyperlink"))
{
using (FileStream fs = new FileStream(fullFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
UriFixer.FixInvalidUri(fs, brokenUri => FixUri(brokenUri));
}
htmlText = ParseDOCX(fileInfo);
}
}
return htmlText;
}
private static string FixUri(string brokenUri)
{
string newURI = string.Empty;
if (brokenUri.Contains("mailto:"))
{
int mailToCount = "mailto:".Length;
brokenUri = brokenUri.Remove(0, mailToCount);
newURI = brokenUri;
}
else
{
newURI = " ";
}
return newURI;
}
private string ParseDOCX(FileInfo fileInfo)
{
byte[] byteArray = File.ReadAllBytes(fileInfo.FullName);
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true))
{
int imageCounter = 0;
var pageTitle = fileInfo.FullName;
var part = wDoc.CoreFilePropertiesPart;
if (part != null)
pageTitle = (string)part.GetXDocument().Descendants(DC.title).FirstOrDefault() ?? fileInfo.FullName;
// TODO: Determine max-width from size of content area.
WmlToHtmlConverterSettings settings = new WmlToHtmlConverterSettings()
{
AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }",
PageTitle = pageTitle,
FabricateCssClasses = true,
CssClassPrefix = "pt-",
RestrictToSupportedLanguages = false,
RestrictToSupportedNumberingFormats = false,
ImageHandler = imageInfo =>
{
++imageCounter;
string extension = imageInfo.ContentType.Split('/')[1].ToLower();
ImageFormat imageFormat = null;
if (extension == "png")
imageFormat = ImageFormat.Png;
else if (extension == "gif")
imageFormat = ImageFormat.Gif;
else if (extension == "bmp")
imageFormat = ImageFormat.Bmp;
else if (extension == "jpeg")
imageFormat = ImageFormat.Jpeg;
else if (extension == "tiff")
{
// Convert tiff to gif.
extension = "gif";
imageFormat = ImageFormat.Gif;
}
else if (extension == "x-wmf")
{
extension = "wmf";
imageFormat = ImageFormat.Wmf;
}
// If the image format isn't one that we expect, ignore it,
// and don't return markup for the link.
if (imageFormat == null)
return null;
string base64 = null;
try
{
using (MemoryStream ms = new MemoryStream())
{
imageInfo.Bitmap.Save(ms, imageFormat);
var ba = ms.ToArray();
base64 = System.Convert.ToBase64String(ba);
}
}
catch (System.Runtime.InteropServices.ExternalException)
{ return null; }
ImageFormat format = imageInfo.Bitmap.RawFormat;
ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders().First(c => c.FormatID == format.Guid);
string mimeType = codec.MimeType;
string imageSource = string.Format("data:{0};base64,{1}", mimeType, base64);
XElement img = new XElement(Xhtml.img,
new XAttribute(NoNamespace.src, imageSource),
imageInfo.ImgStyleAttribute,
imageInfo.AltText != null ?
new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);
return img;
}
};
XElement htmlElement = WmlToHtmlConverter.ConvertToHtml(wDoc, settings);
var html = new XDocument(new XDocumentType("html", null, null, null), htmlElement);
var htmlString = html.ToString(SaveOptions.DisableFormatting);
return htmlString;
}
}
}
}
这是上面代码使用的urifixer类
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
public static class UriFixer
{
public static void FixInvalidUri(Stream fs, Func<string, string> invalidUriHandler)
{
XNamespace relNs = "http://schemas.openxmlformats.org/package/2006/relationships";
using (ZipArchive za = new ZipArchive(fs, ZipArchiveMode.Update))
{
foreach (var entry in za.Entries.ToList())
{
if (!entry.Name.EndsWith(".rels"))
continue;
bool replaceEntry = false;
XDocument entryXDoc = null;
using (var entryStream = entry.Open())
{
try
{
entryXDoc = XDocument.Load(entryStream);
if (entryXDoc.Root != null && entryXDoc.Root.Name.Namespace == relNs)
{
var urisToCheck = entryXDoc
.Descendants(relNs + "Relationship")
.Where(r => r.Attribute("TargetMode") != null && (string)r.Attribute("TargetMode") == "External");
foreach (var rel in urisToCheck)
{
var target = (string)rel.Attribute("Target");
if (target != null)
{
try
{
Uri uri = new Uri(target);
}
catch (UriFormatException)
{
string newUri = invalidUriHandler(target);
rel.Attribute("Target").Value = newUri;
replaceEntry = true;
}
}
}
}
}
catch (XmlException)
{
continue;
}
}
if (replaceEntry)
{
var fullName = entry.FullName;
entry.Delete();
var newEntry = za.CreateEntry(fullName);
using (StreamWriter writer = new StreamWriter(newEntry.Open()))
using (XmlWriter xmlWriter = XmlWriter.Create(writer))
{
entryXDoc.WriteTo(xmlWriter);
}
}
}
}
}
}
使用系统;
使用System.IO;
使用系统IO压缩;
使用System.Linq;
使用System.Xml;
使用System.Xml.Linq;
公共静态类修复程序
{
公共静态void FixInvalidUri(流fs、Func invalidUriHandler)
{
XRELNS=”http://schemas.openxmlformats.org/package/2006/relationships";
使用(ZipArchive za=new ZipArchive(fs,ZipArchiveMode.Update))
{
foreach(za.Entries.ToList()中的var条目)
{
如果(!entry.Name.EndsWith(“.rels”))
继续;
bool replaceEntry=false;
XDocument entryXDoc=null;
使用(var entryStream=entry.Open())
{
尝试
{
entryXDoc=XDocument.Load(entryStream);
if(entryXDoc.Root!=null&&entryXDoc.Root.Name.Namespace==relNs)
{
var urisToCheck=entryXDoc
.后代(relNs+“关系”)
其中(r=>r.Attribute(“TargetMode”)!=null&&(string)r.Attribute(“TargetMode”)==“External”);
foreach(urisToCheck中的var rel)
{
var target=(字符串)相对属性(“target”);
如果(目标!=null)
{
尝试
{
Uri=新Uri(目标);
}
捕获(异常)
{
字符串newUri=invalidUriHandler(目标);
相对属性(“目标”).Value=newUri;
replaceEntry=true;
}
}
}
}
}
捕获(XmlException)
{
继续;
}
}
如果(替换条目)
{
var fullName=entry.fullName;
entry.Delete();
var newEntry=za.CreateEntry(全名);
使用(StreamWriter=newstreamwriter(newEntry.Open()))
使用(XmlWriter=XmlWriter.Create(writer))
{
entryXDoc.WriteTo(xmlWriter);
}
}
}
}
}
}
您所要做的就是为ConvertToHtml方法提供.docx文件的文件路径,它将为您完成工作并提供html
注意:这只支持.docx如果您想避免与转换为(X)HTML相关的问题,您可以尝试一下(披露:我对该公司感兴趣)。原生文档侧重于提供与Word本身相当的逼真度,同时便于开发人员嵌入到Web应用程序中。这真的很有帮助。但这并不是只在编辑器中打开文件。这也是关于以相同的格式将其保存回系统。是的,您肯定也可以这样做。做结帐,这里有一个我刚刚创建的小样本项目:谢谢Ozesh。我会检查一下。仅供参考:本机文档网站上的演示不起作用??我正在寻找一个类似的组件。你有工作演示吗?现在重新工作;我们的磁盘空间不足。