[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2014-02-19
10
Medium Priority
?
414 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
Technology Partners: 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!

 

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

649 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