2

Hi I implemented upload max file size validation using jQuery and it is working fine in
chrome and firefox not working in IE

$('#file').change(function() {           
         fileSizeError = (this.files[0].size/(1024*1024) > 1) ? true : false;
    });

please help me how to resolvw this problem

After googling i found one solution that is to create an ActiveXObject.The code is below

var myFSO = new ActiveXObject("Scripting.FileSystemObject");
        var filepath = document.upload.file.value;
        var thefile = myFSO.getFile(filepath);
        var size = thefile.size;
        alert(size + " bytes"); 

But iam unable to create an ActiveXObject my objective is this code is work on IE and other browsers also.

user2590163
  • 73
  • 1
  • 4
  • 12

1 Answers1

0
**You can check this file size in server side using jquery webmethod**

As stated by hoodedperson70 here:

Sure thing, here's an example, but I cannot test...it should work though:

<html>
<head>
  <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
  <script type="text/javascript" src="ajaxfileupload.js"></script>
  <script type="text/javascript">
      $(document).ready(function () {
          $("#submit1").click(function () {
              var file == $("#file1");
              if (file.value = "" || file.value == null)
              {
                  alert("You must provide a file.");
              }
              else
              {
                  ajaxFileUpload();
              }
          });
      });

      function ajaxFileUpload()
      {
          //starting setting some animation when the ajax starts and completes
          $("#loading").ajaxStart(function () {
              $(this).show();
          })
          .ajaxComplete(function () {
              $(this).hide();
          });

          /*
              preparing ajax file upload
              url: the url of script file handling the uploaded files
              fileElementId: the file type of input element id
              dataType: it supports json, xml
              secureuri: use secure protocol
              success: call back function when the ajax complete
              error: callback function when the ajax failed
          */
          $.ajaxFileUpload
          (
              {
                  url: 'THE WEB SERVICE URL',
                  secureuri: false,
                  fileElementId: 'file1',
                  dataType: 'json',
                  success: function (data, status)
                  {
                      if (data.success == "true")
                      {
                          $("#results").html("Valid file size, " + data.length);
                      }
                      else
                      {
                          $("#results").html("Invalid file size, " + data.length);
                      }
                  },
                  error: function (data, status, e)
                  {
                      alert(data + ", " + e);
                  }
              }
          )

          return false;
      }
  </script>
</head>
<body>
  <form id="form1" method="POST" action="" enctype="multipart/form-data">
      <input type="file" id="file1" name="file1" />
      <input type="button" id="submit1" name="submit1" value="Upload (size will be checked)" />
      <img src="loading.gif" alt="Loading" id="loading" style="display:none;" />
      <br /><br /><br />
      <div id="results"></div>
  </form>
</body>
</html>

And here's the web service code:

[WebMethod]
public string CheckFileSize(HttpContext context)
{
  var file1 = context.Request.Files[0];

  if (file1 != null && file1.ContentLength > 0)
  {
      if (file1.ContentLength > 1024) // This is the IMPORTANT comparison
      {
          return "{\"success\":\"true\",\"length\":\"" + file1.ContentLength + "\"}";
      }
      else
      {
          return "{\"success\":\"false\",\"length\":\"" + file1.ContentLength + "\"}";
      }
  }
}

You will need to follow this link: http://www.phpletter.com/download_project_version.php?version_id=34 to download the .js file and the "loading.gif" file. For my example, they are in the same directory, but you may not have that, so you'll need to change their paths if necessary.

I hadn't realized they use PHP as the processing, but this should still work just the same. The javascript should be able to submit the file to any type of technology, so it depends on the web service logic. It's very possible that the use of HttpContext isn't right for this scenario, but you can test this out.

More than way's for client side validation

else , simply try this way

if (FileUpload1.PostedFile.ContentLength > 5120) // 5120 KB means 5MB
            {
                //Display a warning
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big')", true);
            }
            else
            {
                //save the file here


            }
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • When you copy the words of others, you must enclose them in quotes to indicate that they are not your words. You can't just throw a link at the bottom of a post. I've edited the above to reflect this, but you must do this in the future. – Brad Larson Aug 01 '13 at 22:29