webodf / ViewerJS

ViewerJS: Document Reader in JavaScript
http://viewerjs.org
1.94k stars 421 forks source link

blob url not read on chrome/IE but works on Firefox #230

Open bwallrich opened 7 years ago

bwallrich commented 7 years ago

when the displayed file is in a blob (in the navigator memory) an url like

http://localhost/ViewerJS/#blob:http://localhost/b80b04ce-6f03-40c7-ae4a-504428ffed7f

works fine on firefox. But nothing displayed on Chrome or IE. (in both cases i'm sure the blob is filled)

Chrome : Version 54.0.2840.71 unknown (64-bit) IE :11/Edge

bwallrich commented 7 years ago

The problem comes from the

xhr.open("HEAD", documentUrl, true);

(file PluginLoader.js)

which doesn't answer a xhr.status == 2 on chrome for a blob, and never fill an answer to xhr.getResponseHeader("content-type"). You must do a

xhr.open("GET", documentUrl, true);

instead to and abort when you obtain header.

It work with this new function :

xhr.onreadystatechange = function() {
            var mimetype, matchingPluginData;
            if (xhr.readyState !== 1) {
                mimetype = xhr.getResponseHeader('Content-Type');
                if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 0) {
                    mimetype = xhr.getResponseHeader('Content-Type');
                    if (mimetype) {
                        pluginRegistry.some(function(pluginData) {
                            if (pluginData.supportsMimetype(mimetype)) {
                                matchingPluginData = pluginData;
                                console.log('Found plugin by mimetype and xhr head: ' + mimetype);
                                return true;
                            }
                            return false;
                        });
                        xhr.abort();
                    }
                }
                cb(matchingPluginData);
            }
        };

This can be integrated ?

mpaccione commented 7 years ago

I need this feature for an application... reading blob url's from the browser, not the server. Will this be implemented soon?