Solved

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

Posted on 2014-02-19
10
395 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Code works but how can I download the file? 20 47
Setting runtime form location 4 19
Hidden Field Value 10 37
Entity Framework 3 11
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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