[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

ASPX: FileUpload limit IN ADVANCE the size and type of files.

Dear experts

I’m relative new to ASPX. I have a page on which a user could upload a file. I want to limit the user by type /image/ and by size (3 MB), but I want do this BEFORE the user post the file. I use a standard FileUpload control, not upload the file in additional page, I upload it on the main Submit button - look the page:
http://demo.asp2.eu/Register_bMA.aspx?langID=EN

I put a code in my Submit button for that purpose, but it look when the user select a big file the code AT all do not goes here – it get server error message. I want to stop user BEFORE he start uploading file with 200 MB by mistake /there is change for such mistake, no body read the message what should be uploaded here and it may start upload video file. /
0
dvplayltd
Asked:
dvplayltd
1 Solution
 
MrunalCommented:
Hi,
if server error is coming then you have to change web.config file...

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="SizeHere" />
  </system.web>
</configuration>
"SizeHere" is in KB. The default is 4096 (= 4 MB).

Another solution is:

http://forums.asp.net/t/1099698.aspx

Hope this helps you.
0
 
dvplayltdAuthor Commented:
An when I change this, the user will get the message that bigger size is not allowed ? I doublt, i think it simple will make the error, I'm right or not ?
0
 
Carl TawnSystems and Integration DeveloperCommented:
You can check the size using javascript with the following (for IE this will only work with IE10 or above):
this.files[0].size

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dvplayltdAuthor Commented:
I think I should use JavaScipt - but I need solution for all major brousers, not only IE.
0
 
Carl TawnSystems and Integration DeveloperCommented:
It works with all browsers. My comment meant that for IE it will only work with 10 and above, all non-IE browsers should be fine.
0
 
dvplayltdAuthor Commented:
Or may be to use AJAX FileUpload control or other third part library? Anyway, i use AJAX library ... i want to put a control which to make validation BEFORE real upload of the file. This is common task, how it is usually solved?
0
 
dvplayltdAuthor Commented:
To Carl Tawn

It is a possible solution, could you please help me with more detail how to apply it? I have a simple form with Submit button and C# code after it, i think I should replace and first call this JavaFunction, but I need help for right syntacs

                        <asp:Button CssClass="submit" ID="btnSubmit" runat="server"
                            ValidationGroup="contact"  OnClick="btnSubmit_Click" Height="38px"
                            Width="118px" />

Also i think it is easy /for you, not for me/ to detect the user browser in this JavaScript function, if it is <than IE10 to turn off the function.
0
 
Alan WarrenCommented:
Hi dvplayltd,
Something like this oughta do the trick for you:
Q_28368497 Snapshot
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default7.aspx.vb" Inherits="Default7" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
  <script type="text/javascript">
    function checkit(obj) {
      var FileName = obj.value;
      var FileExt = FileName.substr(FileName.lastIndexOf('.') + 1).toLowerCase();
      var FileSize = obj.files[0].size;
      var FileSizeMB = (FileSize / 1048576).toFixed(2);

      if ((FileExt != "jpg" && FileExt != "gif" && FileExt != "png") || FileSize > 3145728) {
        var error = "File type : " + FileExt + "\n\n";
        error += "Size: " + FileSizeMB + " MB \n\n";
        error += "Please make sure your file is in jpg, gif or png format and less than 3 MB.\n\n";
        alert(error);
        return false;
      }
      return true;
    }  </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:FileUpload ID="FileUpload1" runat="server" onchange="checkit(this)" />
    </div>
    </form>
</body>
</html>

Open in new window

Respectfully yours,
Alan
0
 
dvplayltdAuthor Commented:
HI Alan,

It look excatly what I need, thanks. I'll test it in few days and I'll guest I'll give you the points
0
 
dvplayltdAuthor Commented:
10x - I already implement it, thanks a lot!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now