FileUpload error Size in file [0]

0

I have an asp control to upload files.

<asp:FileUpload ID="fuAttachmentsSoporte" CssClass="required"  runat="server" ClientIDMode="Static"/>    
<asp:Button ID="btnAttachmentsSoporte" runat="server" Text="Adjuntar" CausesValidation="False" OnClick="btnAttachmentsSoporte_Click" disabled />
<asp:HiddenField ID="HFUploadMaxFileSize" ClientIDMode="Static" runat="server" /><br />
<asp:Label runat="server" ID="lblMensajeError" style="display:none;" CssClass="ms-formvalidation">El archivo seleccionado excede del tamaño máximo permitido.</asp:Label>

Using a javascript function, I control the size of the file, and if it is larger than desired, I indicate it to the user and I do not unblock the button to attach.

function ValidateMaxSize(fileId) {

    var file = document.getElementById(fileId);        
    var fileSize = file.files[0].size / 1024;

    var lblError = file.parentElement.children[4];
    var btn = file.parentElement.children[1];

    lblError.style.display = 'none';        

    var maxFileSize = document.getElementById("HFUploadMaxFileSize").value;

    if (fileSize > maxFileSize) {

        lblError.style.display = 'block';
        btn.disabled = true;

        return false;
    } 
    btn.disabled = false;
    return true;

}

The problem is that if I delete that file and give it to save, I get an error! and the thing is that even if you reset the input or put its value at 0 file[0] still has value ...

Doing the same thing with a small file does not happen to me.

    
asked by GDP 13.12.2017 в 13:57
source

1 answer

1

One way to clean a input of type file is to replace it with a clone of itself:

function ValidateMaxSize(fileId) {

    var file = document.getElementById(fileId);        
    var clon = file.cloneNode();

    var fileSize = file.files[0].size / 1024;

    var lblError = file.parentElement.children[4];
    var btn = file.parentElement.children[1];

    lblError.style.display = 'none';        

    var maxFileSize = document.getElementById("HFUploadMaxFileSize").value;

    if (fileSize > maxFileSize) {
        file.parentNode.replaceChild(clon, file);

        lblError.style.display = 'block';
        btn.disabled = true;

        return false;
    } 
    btn.disabled = false;
    return true;
}
    
answered by 13.12.2017 / 14:22
source