2012-04-04 64 views
0

如何使用jquery測量文件大小,下面的代碼在firefox,chrome中可以正常工作,但它在IE(7/8/9)中不起作用。任何一個可以幫助我如何衡量IE使用jQuery在ASP.NET中測量上傳文件的大小

文件大小
var fi = document.getElementById('loadfile'); 
var sizeInMB = fi.files[0].size; 
    sizeInMB = (sizeInMB/(1024 * 1024)); 
if (sizeInMB > 20) 
    alert("File size more than 20MB"); 
else 
    alert("File size less than 20MB"); 
+1

你看到這個答案? http://stackoverflow.com/a/1440804/882630 – lukkea 2012-04-04 11:07:47

+0

可能的重複[查找文件大小與jQuery](http://stackoverflow.com/questions/1440723/find-file-size-with-jquery) – Gray 2012-04-04 12:37:44

+0

@格雷 - 以前的評論可能重複! – lukkea 2012-04-04 12:58:21

回答

0

試試這個方法:

<script runat="server"> 

    protected void UploadButton_Click(object sender, EventArgs e) 
    { 

     string savePath = @"c:\temp\uploads\"; 


     if (FileUpload1.HasFile) 
     {     
      // Get the size in bytes of the file to upload. 
      int fileSize = FileUpload1.PostedFile.ContentLength; 

      // Allow only files less than 2,100,000 bytes (approximately 2 MB) to be uploaded. 
      if (fileSize < 2100000) 
      { 

       // Append the name of the uploaded file to the path. 
       savePath += Server.HtmlEncode(FileUpload1.FileName); 


       FileUpload1.SaveAs(savePath); 


       UploadStatusLabel.Text = "Your file was uploaded successfully."; 
      } 
      else 
      { 

       UploadStatusLabel.Text = "Your file was not uploaded because " + 
             "it exceeds the 2 MB size limit."; 
      } 
     } 
     else 
     { 

      UploadStatusLabel.Text = "You did not specify a file to upload."; 
     } 
    } 
</script> 
相關問題