Solved

Upload file size validation

Posted on 2010-11-23
4
751 Views
Last Modified: 2012-06-21
Experts,

I have a web form on an intranet site which allows internal users to upload documents and send them as attachments via email. I'm using classic ASP to attain this task. When the user completes the form, the files get uploaded to the server, sent to the user via email, and deleted right away from the server.

I want to validate the size of these files to be uploaded. I realize that this cannot be done using JScript. I've used the getSize() function (see code below) but in vain. Neither IE nor Chrome respond to this code and FF throws "ActiveXObject is not defined" error.

I've also tried using the server side validation and it doesn't work either (code attached below).

Please let me know what other options I can opt for. I'd appreciate any help.

Thanks,
<script type="text/vbscript" language="vbscript">
function getSize(){

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



------------------------------------------------------------

<%
Set Upload = Server.CreateObject("Persits.Upload")
' Limit file size to 50000 bytes, throw an exception if file is larger
Upload.SetMaxSize 50000, True

' Intercept all exceptions to display user-friendly error
On Error Resume Next

' Perform upload
Upload.Save "c:\upload"

' 8 is the number of "File too large" exception
If Err.Number = 8 Then
   Response.Write "Your file is too large. Please try again."
Else
   If Err <> 0 Then
      Response.Write "An error occurred: " & Err.Description
   Else
      Response.Write "Success!"
   End If
End If
%>
</script>

Open in new window

0
Comment
Question by:SasDev
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 34196903
Is any part of this being stored on the db?

You could easily ascertain the size from there.
0
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 34197009
How about this?

<%
Set Upload = Server.CreateObject("Persits.Upload")
' Limit file size to 50000 bytes, throw an exception if file is larger
Upload.SetMaxSize 50000, True

' Intercept all exceptions to display user-friendly error
On Error Resume Next

' Perform upload
Upload.Save "c:\upload"

%>
Files Size<BR>
<%
For Each File in Upload.Files
Response.Write File.Name & "= " & File.Path & " (" & File.Size &" bytes)<BR>"
Next
%>

<P>


' 8 is the number of "File too large" exception
If Err.Number = 8 Then
   Response.Write "Your file is too large. Please try again."
Else
   If Err <> 0 Then
      Response.Write "An error occurred: " & Err.Description
   Else
      Response.Write "Success!"
   End If
End If
%>

Open in new window


This is from Persits site:

http://www.aspupload.com/manual_simple.html
0
 
LVL 1

Assisted Solution

by:SasDev
SasDev earned 0 total points
ID: 34230366
Sorry for not responding to your posts.

Your posts were helpful and the link you provided was of a good help too.

Although I found the resolution to my query on this link: http://www.webmaster-talk.com/asp-forum/110314-how-can-i-add-file-size.html

I appreciate your help.

Thanks
0
 
LVL 1

Author Closing Comment

by:SasDev
ID: 34272717
Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA taking too long 5 24
key press alert 2 32
How to implement an additional command 16 40
Excel 2007 Macro to Change Column Formatting 3 37
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question