Converting docx to html with dotnet-mammoth fails

2019-03-06 01:41发布

I'm using dotnet-mammoth (mammoth.js with edge.js) to convert a docx document to html in .net
I added it to my project via its nuget package.

I'm using the code provided by the sample, which is working correctly in my development enviroment (running IIS Express):

var documentConverter = new Mammoth.DocumentConverter();
var result = documentConverter.ConvertToHtml(Server.MapPath("~/files/document.docx")); // problem here at production enviroment
string theResult = result.Value

However, once I deploy it to production server, when the executed code reaches documentConverter.ConvertToHtml() method, it's redirecting me to the login page. Without displaying any error messages, without saving anything on IIS log file.

If I remove that line, everything else executes normally. I assume it could be an issue related to permissions but I don't know what could it be. Any ideas?

2条回答
倾城 Initia
2楼-- · 2019-03-06 02:10

You can resolve this by getting the exact error when the process is trying to read the file. Below is the code from dotnet-mammoth DocumentConverter.cs. As shown below on call it is trying to read all bytes to be sent to edge

public Result<string> ConvertToHtml(string path)
        {
            var mammothJs = ReadResource("Mammoth.mammoth.browser.js") + ReadResource("Mammoth.mammoth.edge.js");

            var f = Edge.Func(mammothJs);
            var result = f(File.ReadAllBytes(path));
            Task.WaitAll(result);

            return ReadResult(result.Result);
        }

I suppose you are giving absolute path to the input. In that case the absolute path should be accessible by app identity hosting the app pool of the web application.

If the path specified is in web root directory - (not advised) - but if it is then you can use Server.MapPath

查看更多
神经病院院长
3楼-- · 2019-03-06 02:21

The latest version of Mammoth on NuGet no longer uses edge.js, and is now just .NET code, so should work more reliably.

查看更多
登录 后发表回答