对于上一篇所说的公开平台,有个功能是发布公开信息。原本这功能的操作是,在一个TextArea(在线编辑器如xheditor)里编写内容,编写文本、上传图片等,然后保存把TextArea中的内容存入数据库。当需要浏览该信息时,在网页上显示。这样挺好的,很多网站都是这么做的,包括ITEYE写blog时,也是在这在线编辑器中编辑的。但是客户说了,每次要上传图片都要一张一张地上传,太麻烦了啊,能够把word文档中的内容直接复制到TextArea就好了。对于此,word文档中复制文本过来是没问题的,但是复制图片过来,问题就大了,在线编辑器不能把复制粘贴过来的图片自动上传到服务器啊,就算是HTML5上传也不行啊,我不会弄啊,google了好久也不会啊……腾讯的QQ邮箱可以做到复制word文档里的图片,直接粘贴到邮件正文里。他是怎么做到的啊!GMAIL都木有这功能啊!
既然以上所说的功能不能实现(小弟实力有限唉),那退而求其次,应该可以用其他方法替代,最坏就是大不了跟客户说这功能做不了,咋地吧……我想了想,word文档有个“另存为”功能,把doc转换为html的。那能不能让客户上传word文档,系统把doc转为html,保存好。当群众查询信息时,直接展示这个html不就好了!好,就这么办!
首先,在asp页面上添加上传文件的file标签:
<tr>
<td align="right">上传word文档:</td>
<td colspan="2"><input type="file" id="file1" runat="server" /> </td>
</tr>
然后编写后台代码,在用户提交时对file1进行处理。
在C#源文件的开头,要添加using微软office的东西:
using WORD=Microsoft.Office.Interop.Word;
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using WORD=Microsoft.Office.Interop.Word;//要添加这个,用于处理word
对于Microsoft.Office.Interop.Word,有个傻*问题折腾了我大半个早上,唉……新手
当我添加Microsoft.Office.Interop.Word时,Visual studio提示:命名空间“Microsoft.Office”中不存在类型或命名空间名称“Office"。Google了好久,又是修复OFFICE2007,又是引入COM中的com/Microsoft Office 12.0 Object Library,都不行啊,要么提示不存在“Office”,要么提示不存在“Interop”。最后只能百度知道了,没想到百度知道的第一个搜索结果就解决了这个问题。只要在项目的Bin中引入以下DLL文件就可以了:Microsoft.Office.Interop.Word.dll,可以从附件下载。
解决这个问题后,就可以写btnOK_Click方法,来处理word文档了。
贴上代码:
protected void btnOK_Click(object sender, EventArgs e)
{
if (file1!=null&&file1.PostedFile!=null&&file1.PostedFile.ContentLength > 0)
{
//上传word文档
HttpPostedFile wordFile = file1.PostedFile;
string wordFileName = wordFile.FileName.ToLower();
string fileExt = Path.GetExtension(wordFileName).ToLower();
//判断扩展名
if (String.IsNullOrEmpty(fileExt) || (fileExt!=".doc"&&fileExt!=".docx") )
{
//TODO 提示 上传失败!只能上传后缀名为doc或docx的word文档。" 并返回
}
//判断文件大小
if (wordFile.InputStream == null || wordFile.InputStream.Length > maxSize)
{
//TODO 提示 上传失败!上传的文件过大 并返回
}
//把word文档保存在服务器中
string randomFileName = WebBase.GetNewNo();//随机生成文件名(WebBase是自己写的一个工具类)
string newFileName = randomFileName + fileExt;//保存在服务器上的word文档的文件名
string localPath = WebBase.ServerMapPath(dirPath + imgPath);//保存在服务器上的文件夹的路径
if (!Directory.Exists(localPath))
{
Directory.CreateDirectory(localPath);
}
wordFile.SaveAs(localPath + newFileName);
//把word文档转化为html
WORD.ApplicationClass word = new WORD.ApplicationClass();
Type wordType = word.GetType();
WORD.Documents docs = word.Documents;
// 打开文件
Type docsType = docs.GetType();
//打开一个Word文档
string wordPathInSever = localPath + newFileName;
Microsoft.Office.Interop.Word.Document doc = (WORD.Document)docsType.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordPathInSever, true, true });
// 转换格式,另存为html
Type docType = doc.GetType();
string htmlFileName = randomFileName + ".html";
//被转换的html文档保存的位置
object saveFileName = localPath + htmlFileName;
//保存
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { saveFileName, WORD.WdSaveFormat.wdFormatFilteredHTML });
//一定要先关闭,否则退出Word时不能释放资源
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
//退出Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
}
}
这样就成功把word转化为html了,然后再往数据库中记录一些信息(如保存的html的路径等),当用户查询时,根据这些信息找到html页面,直接显示这html就可以了。