Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. size of fileupload

size of fileupload

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    ptvce
    wrote on last edited by
    #1

    Hi i have a fileupload control in form, and now i want to check the size of file before upload it. i wrote this code but the value of fileSize = 0 all the time. what should i do. can anybody help me? int fileSize = fuSignature.PostedFile.ContentLength

    L P 2 Replies Last reply
    0
    • P ptvce

      Hi i have a fileupload control in form, and now i want to check the size of file before upload it. i wrote this code but the value of fileSize = 0 all the time. what should i do. can anybody help me? int fileSize = fuSignature.PostedFile.ContentLength

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      ptvce wrote:

      before upload

      You code seems to be the server-side code and will be executed AFTER the file is uploaded. To check the file size before uploading the file, you've to use client side code i.e. JavaScript. But JavaScript does not allow accessing file system due to security reasons. Here[^] is an inelegant approach to do this if you are targeting only IE.

      1 Reply Last reply
      0
      • P ptvce

        Hi i have a fileupload control in form, and now i want to check the size of file before upload it. i wrote this code but the value of fileSize = 0 all the time. what should i do. can anybody help me? int fileSize = fuSignature.PostedFile.ContentLength

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        That code doesn't work because ASP.NET is a server side technology. In other words, you have to transfer the posted file to the server before it can work out how big it is. There are things that you can do, but they generally involve compromises - one trick is to use a flash uploader control which can get access to the users hard disk to retrieve this information. Alternatively, if you know that your clients aren't going to be using Internet Explorer, you can use this function:

        <script type='text/javascript'>
        function showFileSize() {
        var input, file;
        if (typeof window.FileReader !== 'function') {
        alert('The file API isn't supported by your browser');

        return;     
        

        }
        input = document.getElementById('fileinput'); // Or whatever name your input control is...
        if (!input) {
        alert('You don't seem to have a file input control');
        return;
        }
        if (!input.files) {
        alert('This browser doesn't seem to support files.');
        return;
        }
        if (!input.files[0]) {
        alert('You havent picked a file');
        return;
        }

        file = input.files[0];
        alert("The file is " + file.size + " bytes");
        </script>

        If your clients have IE, you may have to use an ActiveX control. A final note - you really should have posted this in the ASP.NET forum.

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups