html value is truncating my string

2019-05-19 08:07发布

问题:

On my page I am using HTML5 to upload a file on my browser. The file is a PDF an have to show it in a <input type="text"/>

This is my code:

var files = evt.target.files; // FileList object

// files is a FileList of File objects. List some properties.
for (var i = 0, f; f = files[i]; i++) {
    console.log(f.name);
    console.log(f.type);
    console.log(f.size);

    //Update document info
    $("#document_filename").find("input").val(f.name);
    $("#document_mimetype").find("input").val(f.type);


    var reader = new FileReader();

    // Closure to capture the file information.
    reader.onloadend = (function(theFile) {
        return function(e) {
        content = e.target.result.replace(/^data:[^;]*;base64,/, "");
        console.log(content.length);
        $("#document_content").find("input").val(content);
        a = document.getElementById("document_content").getElementsByTagName("input")[0];
        a.value = content;
        console.log(a.value.length);
        };
    })(f);

    // Read in the image file as a data URL.
            reader.readAsDataURL(f);

}

When I upload a big file 1.5MB, my console is showing:

    contrat.pdf             // The file name
    application/pdf         // The file type
    1510788                 // The file size
    2014384                 // The length of the b64 content
    524288                  // The length of string once inserted in the input box??

Can someone explain to me what I am doing wrong to get this data truncated?

Thanks,

回答1:

As stated on this page, the maximum length possible for an input of type text seems to be 524288 at least on Chrome. And this is what you have...

I suggest you storing your value in another place!


EDIT :

The implementation varies across browsers : on FF (v32), I can have length greater than 524288 (I have tested up to 10.000.000), but on Chrome it is limitated to 524288 even though we increase the maxlength attribute.

There is also an open ticket for WebKit about this issue : https://bugs.webkit.org/show_bug.cgi?id=44883