简体   繁体   中英

Access attached file content using ADODB.Stream in Javascript

I have a Backbone.js project working, where I get a file that the user attaches in a form (a input element named files in my html template) and assign the content to a Backbone Model (instead of sending the file directly).

It works great on Chrome and Firefox using FileReader:

var file_list = $('#device_file').prop('files');
var file_object = file_list[0];
reader = new FileReader();
reader.onload = function(event) {
                var contents = event.target.result;
                self.model.set("file_data", contents);
                self.model.set('_completed', true);
                self.onStartImport();
            };
reader.readAsBinaryString(file_object);

but now I need to make it compatible with old versions of Internet Explorer. IE10 has partial support for FileReader, but older versions do not.

I've tried to use ActiveX objects, like ADODB.Stream, the problem I have is that, without having the complete file path (which seems to be hidden for security reasons), I can't access the file content to assign it to the model property I need. I can get the filename, but not the path.

The only solution I can think of, is to ask IE users to put the file to be uploaded into a "known" folder, like "C:\\MyAppName\\Files" or something similar, but that feels wrong an inelegant.

Any suggestion is appreciated :-)

At the end I realized that on IE, you can in fact get the full path of the attached file (in Chrome, you get a fake path). So this solved my issue in IE:

var fileName = $('#device_file').val();
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(fileName, 1);
var raw_content = f.ReadAll();
self.model.set("file_data", raw_content);

Off course, I added some feature detection to use FileReader if available (from real browsers).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM