使用html5文件api在javascript中读取本地二进制文件

时间:2015-08-04 14:57:34

标签: javascript html5 fileapi

我的问题是,我使用javascript从本地二进制文件(pdf文件)中读取的内容并不完全是文件的真实内容。有微妙但重要的差异。我想这是html5文件api中readAsBinaryString实现中的一个错误。

这是html代码:

<html>
    <head>
        <title>read local file</title>
        <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
        <script src="test.js"></script>
    </head>
    <body>
        <input type="file" id="fileInput">
    </body>
</html>

这是test.js文件的内容:

var clickLoadPdf = function (file) {
    if (file.name.match(/\.pdf$/)) {

        //create Reader
        var reader = new FileReader();

        // Closure to capture the file information.
        reader.onload = (function(theFile) {
            return function(e) {

                //this is the file's content (or should be)
                var content = e.target.result;
                e=e; //set a breakpoint on this instruction


                //call a function where `content` is
                //sent to the server via AJAX

                //sendToServer(content);

            };

        })(file);

        // Read in the pdf file as a binary string.
        reader.readAsBinaryString(file);
    }
};

$(document).ready(function(){
    //attach eventhandler
    $('#fileInput').on('change', function(evt) {
        evt.preventDefault();
        evt.stopPropagation();
        if (evt.target && evt.target.files.length) {
            clickLoadPdf(evt.target.files[0]);
        }
    });
});

将html文件加载到浏览器中(我在Mac上使用Safari),并检查元素。在调试器中,在JavaScript文件中找到注释//set a breakpoint on this instruction并在其上设置断点。然后在浏览器中找到html文件的表示,单击输入按钮并选择任何pdf文件。

断点上方的一行变量content现在应该包含pdf文件的内容,并且在第一个视图中,它们看起来都很好。但当我将其上传到我的服务器并将其存储在那里时,结果是一个pdf文件,可以打开,但是已损坏,因为它只显示没有任何内容的白页。

我正在搜索错误,并发现,在JavaScript文件中,html5文件api没有返回本地pdf文件的100%确切内容。缺少一些字节。

看一下pdf文件开头的这个例子。

这是原始文件(请注意此处显示的最后一行,即xœí½):

%PDF-1.3
%âãÏÓ
1 0 obj
<</CreationDate (D:20131210124436)/Creator (AS-PDF 0.3.0 by Anton Scheffer)/Producer (running on Oracle Database 11g Release 11.2.0.3.0 - 64bit Production)/Title <FEFF>/Author <FEFF>/Subject <FEFF>/Keywords <FEFF>>>
endobj
2 0 obj
<</Type/Font/Subtype/Type0/Encoding/Identity-H/BaseFont/UMHTEK+ZurichBT-Roman/DescendantFonts 3 0 R/ToUnicode 10 0 R>>
endobj
3 0 obj
[4 0 R]
endobj
4 0 obj
<</Type/Font/Subtype/CIDFontType2/CIDToGIDMap/Identity/DW 1000/BaseFont/UMHTEK+ZurichBT-Roman/CIDSystemInfo 5 0 R/W 6 0 R/FontDescriptor 7 0 R>>
endobj
5 0 obj
<</Ordering(Identity) /Registry(Adobe) /Supplement 0>>
endobj
6 0 obj
[0 [600] 3 [276 370] 11 [314 314 527] 15 [276 331 276 300 553 553 553 553 553 553 553 553 553 553 370] 35 [1000 712 641 662 723 604 549 744 713 263 527 649 535 909 733 770 603 770 647 655 603 728 657 977] 61 [602] 68 [530 602 516 602 550 308 592 587 250 250 496 250 912 587 602 600] 85 [370 491 316 587 542 810 545] 93 [438] 98 [712] 103 [770] 108 [530] 124 [602] 129 [587] 137 [644] 178 [500] 180 [437] 197 [437]]
endobj
7 0 obj
<</Type/FontDescriptor/FontName/UMHTEK+ZurichBT-Roman/Flags 32/FontBBox [-167 -236 1265 963]/ItalicAngle 0/Ascent 963/Descent -236/CapHeight 963/StemV 50/FontFile2 8 0 R>>
endobj
8 0 obj
<</Filter /FlateDecode /Length 29200/Length1 43660>>
stream
xœí½

这是JavaScript-variabel content的内容:

%PDF-1.3
%âãÏÓ
1 0 obj
<</CreationDate (D:20131210124436)/Creator (AS-PDF 0.3.0 by Anton Scheffer)/Producer (running on Oracle Database 11g Release 11.2.0.3.0 - 64bit Production)/Title <FEFF>/Author <FEFF>/Subject <FEFF>/Keywords <FEFF>>>
endobj
2 0 obj
<</Type/Font/Subtype/Type0/Encoding/Identity-H/BaseFont/UMHTEK+ZurichBT-Roman/DescendantFonts 3 0 R/ToUnicode 10 0 R>>
endobj
3 0 obj
[4 0 R]
endobj
4 0 obj
<</Type/Font/Subtype/CIDFontType2/CIDToGIDMap/Identity/DW 1000/BaseFont/UMHTEK+ZurichBT-Roman/CIDSystemInfo 5 0 R/W 6 0 R/FontDescriptor 7 0 R>>
endobj
5 0 obj
<</Ordering(Identity) /Registry(Adobe) /Supplement 0>>
endobj
6 0 obj
[0 [600] 3 [276 370] 11 [314 314 527] 15 [276 331 276 300 553 553 553 553 553 553 553 553 553 553 370] 35 [1000 712 641 662 723 604 549 744 713 263 527 649 535 909 733 770 603 770 647 655 603 728 657 977] 61 [602] 68 [530 602 516 602 550 308 592 587 250 250 496 250 912 587 602 600] 85 [370 491 316 587 542 810 545] 93 [438] 98 [712] 103 [770] 108 [530] 124 [602] 129 [587] 137 [644] 178 [500] 180 [437] 197 [437]]
endobj
7 0 obj
<</Type/FontDescriptor/FontName/UMHTEK+ZurichBT-Roman/Flags 32/FontBBox [-167 -236 1265 963]/ItalicAngle 0/Ascent 963/Descent -236/CapHeight 963/StemV 50/FontFile2 8 0 R>>
endobj
8 0 obj
<</Filter /FlateDecode /Length 29200/Length1 43660>>
stream
xí½

区别在于此处显示的最后一行。在原始文件中,它以xœí½开头,但变量包含xí½因此œx之间的í消失了。 xí½正是我在服务器上存储的文件中找到的。

我该如何解决这个问题?

是否还有另一种方法可以通过选择将pdf文件自动上传到服务器?

0 个答案:

没有答案