为了解决此问题,这几天找了很多种方法,包括在网页中不用fileupload来打开电脑中的文件
(不得不说web还是存在一定弊端的)
包括在IIS管理器中设置目录浏览功能(虽然有一定的进展,但是目前还是没能攻破这一难处),话不多说先上主题。
首先可以看一下word中显示的内容:
网页中呈现的图示:
可以看到文档中的文字、数学公式以及图片都能转化出来。格式还没有彻底的测试,但是可以肯定不会有大的变化。。
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <configSections> <sectionGroup name="bundleTransformer"> <section name="core" type="BundleTransformer.Core.Configuration.CoreSettings, BundleTransformer.Core" /> </sectionGroup> </configSections> <system.web> <compilation debug="true" targetFramework="4.7.2" /> <httpRuntime targetFramework="4.7.2" /> </system.web> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" /> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" /> </compilers> </system.codedom> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-5.2.7.0" newVersion="5.2.7.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> </dependentAssembly> </assemblyBinding> </runtime> <appSettings> <add key=" aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <bundleTransformer xmlns="http://tempuri.org/BundleTransformer.Configuration.xsd"> <core> <css> <translators> <add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" /> </translators> <postProcessors> <add name="UrlRewritingCssPostProcessor" type="BundleTransformer.Core.PostProcessors.UrlRewritingCssPostProcessor, BundleTransformer.Core" useInDebugMode="false" /> </postProcessors> <minifiers> <add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" /> </minifiers> <fileExtensions> <add fileExtension=".css" assetTypeCode="Css" /> </fileExtensions> </css> <js> <translators> <add name="NullTranslator" type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" enabled="false" /> </translators> <minifiers> <add name="NullMinifier" type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" /> </minifiers> <fileExtensions> <add fileExtension=".js" assetTypeCode="JavaScript" /> </fileExtensions> </js> </core> </bundleTransformer></configuration>
web.config中有一点题外话,如下列代码
<appSettings> <add key=" aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings>
是用来解决WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping问题的,需要删除下列多余的内容,这个问题是出现在我想在页面中添加验证控件时候出现的
删除后依旧没解决
最后是在后台的cs文件page_load中加入了UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;代码才得以解决
页面中的代码仅需一个按钮Button即可。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="网络上传图片.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> <br /> </div> </form> </body> </html>
cs文件中的代码
这里可以看到使用了using Word = Microsoft.Office.Interop.Word;标红需要在Visual Studio Installer中下载关于office的相关内容。
using System; using System.Collections; using System.Configuration; using System.Data; using System.IO; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Word = Microsoft.Office.Interop.Word; namespace 网络上传图片 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { UnobtrusiveValidationMode = UnobtrusiveValidationMode.None; } protected void Button1_Click(object sender, EventArgs e) { WordToHtml("E:\\word测试.doc"); } /// <summary> /// word转成html /// </summary> /// <param name="wordFileName"></param> private string WordToHtml(object wordFileName) { //在此处放置用户代码以初始化页面 Word.ApplicationClass word = new Word.ApplicationClass(); Type wordType = word.GetType(); Word.Documents docs = word.Documents; //打开文件 Type docsType = docs.GetType(); Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true }); //转换格式,另存为 Type docType = doc.GetType(); string wordSaveFileName = wordFileName.ToString(); string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html"; object saveFileName = (object)strSaveFileName; docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML }); docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null); //退出 Word wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); return saveFileName.ToString(); } } }
这样就可以实现了,但是目前的弊端就是得在cs文件中用WordToHtml(“E:\word测试.doc”);代码
这需要到后台修改路径才能实现功能,目前正在研究怎样直接在前端打开文件直接转化成HTML,后续研究出我会更新的
发的第一篇文章记录一下我替老师做网站时,在关键步骤卡住并攻破的第一个难题