ibrahim Abdullah
ibrahim Abdullah

Reputation: 93

restrict files upload with specific size

I'm have vb.net web application, I've search on the internet for codes to restrict users from upload files more than 10MB, however, after playing with the code worked when I debug the application within my visual studio.

Now, when I move the debugged files to the server it doesn't work !!! what bother me more is way the code is working fine in google chrome but not in the internet explorer !!!

Internet Explorer version 11.0.22, Google chrome 44.0 version

<%@ Page Language="VB" %>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />
    <title></title>    
</head>
<body>
    <form id="Form1" method="post" runat="server">
			<table>
                <tr>
                    <td width="10%" align="left">
                        <asp:Label Style="z-index: 0" ID="Label10" runat="server" Font-Names="Comic Sans MS" Font-Size="X-Small"> ATTACHMENT (IF AVALIABLE)</asp:Label></td>
                    <td width="90%">
                        <input style="z-index: 0" id="MyFile" type="file" name="myFile" runat="server">
                        <asp:Label Style="z-index: 0" ID="Label17" runat="server" Font-Bold="True" Width="197px" ForeColor="Red">Maximum File Size = 10MB</asp:Label></td>
                    <td width="1%"></td>
                </tr>
                <tr>
                    <td width="10%" align="left"></td>
                    <td width="90%">
                        <asp:Button Style="z-index: 0" ID="Button1" runat="server" Font-Names="Comic Sans MS" Height="32px"
                            Width="112px" Text="SUBMIT" BorderColor="#404040" BorderStyle="Solid" BorderWidth="1px"></asp:Button></td>
                    <td width="1%"></td>
                </tr>
            </table>
    </form>
    <input type="hidden" value="0" name="fisize" id="fisize" />
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript">
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";                
            $(document).ready(function () {
            $('#<%= Page.FindControl("MyFile").ClientID%>').change(function() {
            //$("#MyFile").change(function () {
           //var fi = document.getElementById("MyFile");
            var fi =document.getElementById('<%=Page.FindControl("MyFile").ClientID%>');
                    var dd = fi.files[0].size;
                    if (dd > 10485760) {
                        alert("the selected file size is more than 10MB, please select another file !!");
                        confirm_value.value = "No";
                    }
                    else {
                        alert("okay :)");
                        confirm_value.value = "Yes";
                    }
                    document.forms[0].appendChild(confirm_value);
                });
            });
        </script>
</body>
</html>

Upvotes: 0

Views: 864

Answers (1)

Fl&#225;vio Rodrigues
Fl&#225;vio Rodrigues

Reputation: 54

why you dont use the ASP.NET File Upload control?

Then:

int max = 2097152; // ~2MB = 1024 * 2048
if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLeng­th <= max)
{
   //save

}

Upvotes: 1

Related Questions