0

I just found a working docx to html converter using only javascript on github. The main code which converts docx to html is below. The issue is the page just has a button which on click or drag and choosing a word document, opens it as html. I want to specify a file location in the code so I can load it on the server for loading some documents from computer locally.

Code which converts docx to html and renders :

  <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>DocxJS Example</title>
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
        <script type="text/javascript" src="https://www.docxjs.com/js/build/latest.docxjs.min.js"></script>
    </head>
    <body>
        <input id="inputFiles" type="file" name="files[]" multiple="false">
        <div id="loaded-layout" style="width:100%;height:800px;"></div>
        <script>
            $(document).ready(function(){
                var $inputFiles = $('#inputFiles');
                $inputFiles.on('change', function (e) {
                    var files = e.target.files;
                    var docxJS = new DocxJS();

                    docxJS.parse(
                        files[0],
                        function () {
                            docxJS.render($('#loaded-layout')[0], function (result) {
                                if (result.isError) {
                                    console.log(result.msg);
                                } else {
                                    console.log("Success Render");
                                }
                            });
                        }, function (e) {
                            console.log("Error!", e);
                        }
                    );
                });
            });
        </script>
    </body>
</html>

I tried changing var files = e.target.files; to var files = "C:/sda/path/to/docx"; but that didn't help.

I tried to change

var files = e.target.files; 

to

var files = new Array(new File([""], "sample.docx"));

but it gives me OOXML parse error.

Update:

Lets say I have a file location variable in PHP and I wish to use that instead in the javascript code. How do I do it?

I also checked docx2html javascript code and here is the code for it:

  <!DOCTYPE html>
<html>
<head>
    <script src="index.js"></script>
    <script>
        function test(input){
            require("docx2html")(input.files[0]).then(function(converted){
                text.value=converted.toString()
            })
        }
    </script>
</head>
<body>
    <input type="file" style="position:absolute;top:0" onchange="test(this)">
    <br/>
    <br/>
    <textarea id="text"></textarea>
</body>
</html>

Same issue need input.files[0] here as well

Update: I am trying to use the method mentioned in the comments but encounter some errors:

var fil;
    var getFileBlob = function (url, cb) {
        var xhr = new XMLHttpRequest(); 
        xhr.open("GET", url);
        xhr.responseType = "blob";
        xhr.addEventListener('load', function() {
            cb(xhr.response);
        });
        xhr.send();
};

var blobToFile = function (blob, name) {
        blob.lastModifiedDate = new Date();
        blob.name = name;
        return blob;
};

var getFileObject = function(filePathOrUrl, cb) {
       getFileBlob(filePathOrUrl, function (blob) {
          cb(blobToFile(blob, 'test.docx'));
       });
};

getFileObject('demo.docx', function (fileObject) {
     console.log(fileObject);
     fil = fileObject;
}); 

The error primarily was “Cross origin requests are only supported for HTTP.” before I used https://calibre-ebook.com/downloads/demos/demo.docx instead of just demo.docx in above file path. This however gives another error:

Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

which means chrome cannot load it. It needs to be working on a server. If someone can help providing a fix to make it work offline, let me know. The last method was asynchronous call.

Abhishek Singh
  • 77
  • 2
  • 11
  • You cant just point it to a file on disk, use relative path and read this: https://www.html5rocks.com/en/tutorials/file/xhr2/ – DannyThunder Dec 14 '16 at 09:24
  • @DannyThunder I looked into this : http://stackoverflow.com/questions/8390855/how-to-instantiate-a-file-object-in-javascript and tried the above updated code but it gives me OOXML parse error – Abhishek Singh Dec 14 '16 at 16:05
  • @DannyThunder how do I use a PHP file location stored in a PHP variable with the above script? – Abhishek Singh Dec 15 '16 at 05:12

1 Answers1

0

In the browser, there is a sandbox policy.

It can not access files directly via Path.

Please access the file through drag & drop event or input file change event.

  • This post isn't an actual attempt at answering the question. Please note [StackOverflow doesn't work like a discussion forum](http://stackoverflow.com/tour), it is a Q&A site where every post is either a question or an answer to a question. Posts can also have [comments](http://stackoverflow.com/help/privileges/comment) - small sentences like this one - that can be used to critique or request clarification from an author. This should be either a comment or a [new question](http://stackoverflow.com/questions/ask) – ρяσѕρєя K Jan 18 '17 at 04:30