Solved

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

Posted on 2014-02-19
10
407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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