I have this upload script I'm using and I'd like to add some code that would allow me to decide what size of files will be allowed to be uploaded. Can you help me with this? Thanks. <%@ Language=VBScript %> <% option explicit Response.Expires = -1 Server.ScriptTimeout = 600 %> <!-- #include file="aspupload.asp" --> <% ' **************************************************** ' Change the value of the variable below to the pathname ' of a directory with write permissions, for example "C:\Inetpub\wwwroot" Dim uploadsDirVar uploadsDirVar = "d:\inetpub\webmailasp\database\tempUploads" ' **************************************************** function OutputForm() %> <form name="frmSend" method="POST" enctype="multipart/form-data" action="uploadTester.asp" onSubmit="return onSubmitForm();"> <B>File names:</B><br> File 1: <input name="attach1" type="file" size=35><br> File 2: <input name="attach2" type="file" size=35><br> File 3: <input name="attach3" type="file" size=35><br> File 4: <input name="attach4" type="file" size=35><br> <br> <!-- These input elements are obviously optional and just included here for demonstration purposes --> <B>Additional fields (demo):</B><br> Enter a number: <input type="text" name="enter_a_number"><br> Checkbox values: <input type="checkbox" value="1" name="checkbox_values">-1 <input type="checkbox" value="2" name="checkbox_values">-2<br> <!-- End of additional elements --> <input style="margin-top:4" type=submit value="Upload"> </form> <% end function function TestEnvironment() Dim fso, fileName, testFile, streamTest TestEnvironment = "" Set fso = Server.CreateObject("Scripting.FileSystemObject") if not fso.FolderExists(uploadsDirVar) then TestEnvironment = "<B>Folder " & uploadsDirVar & " does not exist.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions." exit function end if fileName = uploadsDirVar & "\test.txt" on error resume next Set testFile = fso.CreateTextFile(fileName, true) If Err.Number<>0 then TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have write permissions.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions." exit function end if Err.Clear testFile.Close fso.DeleteFile(fileName) If Err.Number<>0 then TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have delete permissions</B>, although it does have write permissions.<br>Change the permissions for IUSR_<I>computername</I> on this folder." exit function end if Err.Clear Set streamTest = Server.CreateObject("ADODB.Stream") If Err.Number<>0 then TestEnvironment = "<B>The ADODB object <I>Stream</I> is not available in your server.</B><br>Check the Requirements page for information about upgrading your ADODB libraries." exit function end if Set streamTest = Nothing end function function SaveFiles Dim Upload, fileName, fileSize, ks, i, fileKey Set Upload = New FreeASPUpload Upload.Save(uploadsDirVar) ' If something fails inside the script, but the exception is handled If Err.Number<>0 then Exit function SaveFiles = "" ks = Upload.UploadedFiles.keys if (UBound(ks) <> -1) then SaveFiles = "<B>Files uploaded:</B> " for each fileKey in Upload.UploadedFiles.keys SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " (" & Upload.UploadedFiles(fileKey).Length & "B) " next else SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system." end if SaveFiles = SaveFiles & "<br>Enter a number = " & Upload.Form("enter_a_number") & "<br>" SaveFiles = SaveFiles & "Checkbox values = " & Upload.Form("checkbox_values") & "<br>" end function %> <HTML> <HEAD> <TITLE>ASP Upload</TITLE> <style> BODY {background-color: white;font-family:arial; font-size:12} </style> <script> function onSubmitForm() { var formDOMObj = document.frmSend; if (formDOMObj.attach1.value == "" && formDOMObj.attach2.value == "" && formDOMObj.attach3.value == "" && formDOMObj.attach4.value == "" ) alert("Please press the browse button and pick a file.") else return true; return false; } </script> </HEAD> <BODY> <br><br> <div style="border-bottom: #A91905 2px solid;font-size:16">Upload files to your server</div> <% Dim diagnostics if Request.ServerVariables("REQUEST_METHOD") <> "POST" then diagnostics = TestEnvironment() if diagnostics<>"" then response.write "<div style=""margin-left:20; margin-top:30; margin-right:30; margin-bottom:30;"">" response.write diagnostics response.write "<p>After you correct this problem, reload the page." response.write "</div>" else response.write "<div style=""margin-left:150"">" OutputForm() response.write "</div>" end if else response.write "<div style=""margin-left:150"">" OutputForm() response.write SaveFiles() response.write "<br><br></div>" end if %> <br><br> </BODY> </HTML> Code (markup):
This is difficult to do as the file input box is heavily protected at the browser end. From memory you have two options, either monitor the stream up to the server and break the connection if it exceeds the limit or just flash for the file input box as it allows you to gather much more information up front. Prefer option 1 personally but would certainly look at using a component rather than trying to write it yourself.
i guess you want to check the file size BEFORE you upload it ( to reserve server resources ), you have to write some java script code: function GetSize(file){var fso=new ActiveXObject("Scripting.FileSystemObject");var f=fso.getFile(file);return fso.getFile(file).size;} Code (markup): this function takes file name and returns file size. it uses windows embed ActiveX to get file size. then you write another function to do calculations: function checkFileSize(ctrl){ //check if file size is > 10 MB if(GetSize(ctrl.value)>10485760)//10 MB=10485760 B { alert('Sorry file size > 10MB'); ctrl.form.reset();//file grater than 10MB then reset form } } Code (markup): simply it takes Fileupload control reference and call GetSize function and compare its result to 10485760(10 MB=10485760 B) if the file size exceed 10 MB,it reset form else do nothing and submit form to server.to use this function put an html button and add this attribute to it onclick="checkFileSize(this.form.File1);".
Thanks for the reply and information. As I'm not well-versed in asp, can you tell me where to add this code to the page I orginally psoted in this thread? Or can you add it to my original posted code? Do I need to add an Active X software somewhere on the server? Also, Sorry but I'm not clear on what to do with this information "to use this function put an html button and add this attribute to it onclick="checkFileSize(this.form.File1);" thanks agin. I look forward to your reply.
nothing is needed. heres your modified code : <%@ Language=VBScript %> <% option explicit Response.Expires = -1 Server.ScriptTimeout = 600 %> <!-- #include file="aspupload.asp" --> <% ' **************************************************** ' Change the value of the variable below to the pathname ' of a directory with write permissions, for example "C:\Inetpub\wwwroot" Dim uploadsDirVar uploadsDirVar = "d:\inetpub\webmailasp\database\tempUploads" ' **************************************************** function OutputForm() %> <form name="frmSend" method="POST" enctype="multipart/form-data" action="uploadTester.asp" onSubmit="return onSubmitForm();"> <B>File names:</B><br> File 1: <input name="attach1" type="file" size=35><input name="attach1chk" type="button" style="width:25px"><br> File 2: <input name="attach2" type="file" size=35><input name="attach2chk" type="button" style="width:25px"><br> File 3: <input name="attach3" type="file" size=35><input name="attach3chk" type="button" style="width:25px"><br> File 4: <input name="attach4" type="file" size=35><input name="attach4chk" type="button" style="width:25px"><br> <br> <!-- These input elements are obviously optional and just included here for demonstration purposes --> <B>Additional fields (demo):</B><br> Enter a number: <input type="text" name="enter_a_number"><br> Checkbox values: <input type="checkbox" value="1" name="checkbox_values">-1 <input type="checkbox" value="2" name="checkbox_values">-2<br> <!-- End of additional elements --> <input style="margin-top:4" type=submit value="Upload"> </form> <% end function function TestEnvironment() Dim fso, fileName, testFile, streamTest TestEnvironment = "" Set fso = Server.CreateObject("Scripting.FileSystemObject") if not fso.FolderExists(uploadsDirVar) then TestEnvironment = "<B>Folder " & uploadsDirVar & " does not exist.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions." exit function end if fileName = uploadsDirVar & "\test.txt" on error resume next Set testFile = fso.CreateTextFile(fileName, true) If Err.Number<>0 then TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have write permissions.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions." exit function end if Err.Clear testFile.Close fso.DeleteFile(fileName) If Err.Number<>0 then TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have delete permissions</B>, although it does have write permissions.<br>Change the permissions for IUSR_<I>computername</I> on this folder." exit function end if Err.Clear Set streamTest = Server.CreateObject("ADODB.Stream") If Err.Number<>0 then TestEnvironment = "<B>The ADODB object <I>Stream</I> is not available in your server.</B><br>Check the Requirements page for information about upgrading your ADODB libraries." exit function end if Set streamTest = Nothing end function function SaveFiles Dim Upload, fileName, fileSize, ks, i, fileKey Set Upload = New FreeASPUpload Upload.Save(uploadsDirVar) ' If something fails inside the script, but the exception is handled If Err.Number<>0 then Exit function SaveFiles = "" ks = Upload.UploadedFiles.keys if (UBound(ks) <> -1) then SaveFiles = "<B>Files uploaded:</B> " for each fileKey in Upload.UploadedFiles.keys SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " (" & Upload.UploadedFiles(fileKey).Length & "B) " next else SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system." end if SaveFiles = SaveFiles & "<br>Enter a number = " & Upload.Form("enter_a_number") & "<br>" SaveFiles = SaveFiles & "Checkbox values = " & Upload.Form("checkbox_values") & "<br>" end function %> <HTML> <HEAD> <TITLE>ASP Upload</TITLE> <style> BODY {background-color: white;font-family:arial; font-size:12} </style> <script> function onSubmitForm() { var formDOMObj = document.frmSend; if (formDOMObj.attach1.value == "" && formDOMObj.attach2.value == "" && formDOMObj.attach3.value == "" && formDOMObj.attach4.value == "" ) alert("Please press the browse button and pick a file.") else return true; return false; } function GetSize(file) { var fso=new ActiveXObject("Scripting.FileSystemObject"); var f=fso.getFile(file); return fso.getFile(file).size; } </script> </HEAD> <BODY> <br><br> <div style="border-bottom: #A91905 2px solid;font-size:16">Upload files to your server</div> <% Dim diagnostics if Request.ServerVariables("REQUEST_METHOD") <> "POST" then diagnostics = TestEnvironment() if diagnostics<>"" then response.write "<div style=""margin-left:20; margin-top:30; margin-right:30; margin-bottom:30;"">" response.write diagnostics response.write "<p>After you correct this problem, reload the page." response.write "</div>" else response.write "<div style=""margin-left:150"">" OutputForm() response.write "</div>" end if else response.write "<div style=""margin-left:150"">" OutputForm() response.write SaveFiles() response.write "<br><br></div>" end if %> <br><br> </BODY> </HTML> Code (markup):
If you're using your own server, there's a setting in IIS for the max upload size. That is the most guaranteed way of limiting the file size, I believe by default it's set at 200k, the variable is called AspMaxRequestEntityAllowed