我使用过CkEditor,我想上传.doc文件。现在,可以将一个简单的文本设置到编辑器,但是当文档具有\ r \ n \ r \ n \ r \ n值时,它会给我一个错误。下面是我的代码,我还附上了价值的截图。
if (Request.Files.Count > 0)
{
string retString = string.Empty;
DocumentManagementSystemEntities db = new DocumentManagementSystemEntities();
var file = Request.Files[0];
string fileExt = System.IO.Path.GetExtension(file.FileName);
if (fileExt == ".docx" || fileExt == "doc")
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Document/"), fileName);
try
{
file.SaveAs(path);
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
object readOnly = true;
object wordPath = path;
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(
ref wordPath,
ref miss,
ref readOnly,
ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss);
for (int i = 0; i < docs.Paragraphs.Count; i++)
{
//retString += "<br/>" + docs.Paragraphs[i + 1].Range.Text.ToString();
retString += docs.Paragraphs[i + 1].Range.Text.ToString();
}
ViewBag.DocumentData = retString.ToString();
ViewBag.result = "Document Successfully Upload!";
}
catch (Exception e)
{
}
}
}
else
{
ViewBag.result = "Document Not Support";
}
我绑定了viewdata中的值并将其提供给CKEditor,如下所示:
@using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table style="margin-top:50px;">
<tr>
<td>
<input type="file" name="file" />
</td>
<td>
<input type="submit" name="Submit" id="Submit" value="Upload" />
</td>
</tr>
</table>
@Html.TextAreaFor(m=>m.Data, new { id= "editor1" })
@*<textarea id="editor1" name="editor1"><p>Initial value.</p></textarea>*@
}
<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
<script src="~/ckeditor/plugins/filebrowser/plugin.js"></script>
<script type="text/javascript">
CKEDITOR.replace('editor1', {
filebrowserBrowseUrl: '/browser/browse.php',
filebrowserUploadUrl: '/uploader/upload.php'
});
var myJsVariable = '@ViewBag.DocumentData'
CKEDITOR.instances.editor1.setData(myJsVariable);
</script>