简体   繁体   English

使用Javascript中的ADODB.Stream访问附件文件的内容

[英]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). 我有一个Backbone.js项目正在工作,在该项目中,我获得了用户以表单形式附加的文件(在我的html模板中名为file的输入元素),并将内容分配给Backbone模型(而不是直接发送文件)。

It works great on Chrome and Firefox using FileReader: 使用FileReader的Chrome和Firefox效果很好:

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. 但是现在我需要使其与Internet Explorer的旧版本兼容。 IE10 has partial support for FileReader, but older versions do not. IE10对FileReader具有部分支持,而较早的版本则没有。

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. 我试图使用ActiveX对象(例如ADODB.Stream),但我遇到的问题是,没有完整的文件路径(出于安全原因,它似乎被隐藏了),我无法访问文件内容以将其分配给我需要的模型属性。 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. 我能想到的唯一解决方案是,请IE用户将要上传的文件放在“已知”文件夹中,例如“ C:\\ MyAppName \\ Files”或类似名称,但这听起来很不雅致。

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). 最后,我意识到在IE上,您实际上可以获取附件的完整路径(在Chrome中,您可以得到伪路径)。 So this solved my issue in IE: 因此,这解决了我在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). 当然,我添加了一些功能检测以使用FileReader(如果有)(从真实的浏览器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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