Solved

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

Posted on 2014-02-19
10
398 Views
Last Modified: 2014-03-30
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
Comment
Question by:dvplayltd
10 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 39870123
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
 

Author Comment

by:dvplayltd
ID: 39870137
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39870252
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:dvplayltd
ID: 39870280
I think I should use JavaScipt - but I need solution for all major brousers, not only IE.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39870299
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
 

Author Comment

by:dvplayltd
ID: 39870307
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
 

Author Comment

by:dvplayltd
ID: 39870583
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
 
LVL 26

Accepted Solution

by:
Alan Warren earned 500 total points
ID: 39963270
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
 

Author Comment

by:dvplayltd
ID: 39965806
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
 

Author Closing Comment

by:dvplayltd
ID: 39965825
10x - I already implement it, thanks a lot!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now