In any case, to determine the file size before downloading?

So we have this problem. The user downloads the file, and if it is above 10 MB, it simply tears off the pages and clears, and no good error occurs to describe what happened. Ideally, we would like to examine the file size when the user selects the file that they want to download, but I do not know if this is possible. Our framework is built using ASP.NET, VB.NET and Javascript (and ExtJS 3.0), and it works in IE.

Anyway to do this?

+4
source share
6 answers

I donโ€™t think there is a way to do this using standard HTML forms.

Take a look at SWFUpload . This will allow you to control the file size.

+4
source

You can set the limit in web configuration, the property is called MaxRequestLength.

Install it in web.config in the httpRuntime section:

<httpRuntime executionTimeout="90" maxRequestLength="4096" /> <-- number of bytes 

This should be inserted under <system.web>

As for checking file size, it's as simple as

 If txtFileUpload.PostedFile.ContentLength > 1024 Then <-- bytes 
+3
source

The code below works in Firefox and Chrome, but the check is not specified in IE and Opera. I think in IE you might need to use ActiveXObject. Taken and slightly modified from here .

 <script type="text/javascript"> var clicked = false; function checkSize() { var node = document.getElementById('file'); var check = node.files[0].fileSize; if (check > 4096) { alert('Your file is too big!'); return false; } } </script> <form enctype="multipart/form-data" action="upload_file.php" method="post" class="body"> Select a file: <input type='file' id='file' name='file'> <input type='submit' value=' Upload File ' onClick='return checkSize()'> </form> 
+3
source

Can you use or tried to use ActiveXObject?

Unconfirmed (probably won't work in IE7 +)

 function checkSize(fileInput, limit){ if(!window.ActiveXObject){ return false; } var oas = new ActiveXObject("Scripting.FileSystemObject"), d = fileInput.value, e = oas.getFile(d), f = e.size; return f <= limit; // in bytes } 
+1
source

You can try swfupload if Flash is an option for you.

0
source

With regular javascript and an HTML file tag, this cannot be done due to security reasons, the page cannot access the user drive, which would have to check the file size.

I saw that with a flash, but I'm not a flash guy.

0
source

Source: https://habr.com/ru/post/1315124/


All Articles