|
How can I add file size limitations?
11-20-2007, 04:52 PM
|
How can I add file size limitations?
|
Posts: 216
|
I have this upload script I'm using and I'd like to add some code that would allow me to determine what size of files will be allowed to be uploaded.
Can you help me with this?
Thanks.
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><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>
|
|
|
|
11-20-2007, 08:41 PM
|
Re: How can I add file size limitations?
|
Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
|
ASP is a server side operation, so the file has to be uploaded before you can determine the file size
The upload class should encapsulate a method for determining the uploaded files size, so you use this to decide whether to save the file or reject it.
The file parameters look to be in an array (according to the code you posted) so at a guess,
if Upload.UploadedFiles(LoopVar).FileSize < MaxFileSize then
' save this file
else
' set an error message
end if
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
11-20-2007, 10:28 PM
|
Re: How can I add file size limitations?
|
Posts: 216
|
Thank you for your reply.
That sounds about right.
I'm not well versed in asp so can I please ask you:
Where would this code fit into the code that I posted?
In place of Max file size I'd put the actual maximum file size I choose, correct?
Where would I put an error message, in the code?
It would send the message to the upload page?
Thanks again.
|
|
|
|
11-21-2007, 05:22 AM
|
Re: How can I add file size limitations?
|
Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
|
MaxFileSize would be a variable that you dimension and set the value of.
Assuming the upload class you are using is this one, to do the file size checking will need another public property declaration to return the file length adding to the class, or write a bit of code to check the file size in the save function.
The error message would be returned by the SaveFiles() function.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
11-21-2007, 02:50 PM
|
Re: How can I add file size limitations?
|
Posts: 216
|
Thank you for your reply.
Yes, that is the upload class I am using.
Is this the right forum to ask for "a bit of code to check the file size in the save function"?
Can I find someone here to add it to the code I've already posted in the beginning of this thread?
Thanks again.
|
|
|
|
11-23-2007, 06:37 AM
|
Re: How can I add file size limitations?
|
Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
|
Not a problem.
file freeASPUpload.asp add this line into the private variable declaration block
Code:
private m_lMaxFileSize
and
Code:
public property let MaxFileSize(ByVal Val)
m_lMaxFileSize = Val
end property
public property get MaxFileSize()
MaxFileSize = m_lMaxFileSize
end property
this goes in property declarations (after Class_Terminate() )
This code has to replace the existing Sub Save()
Code:
Public Sub Save(path)
Dim streamFile, fileItem
if Right(path, 1) <> "\" then path = path & "\"
if not uploadedYet then Upload
For Each fileItem In UploadedFiles.Items
if fileItem.Length < m_lMaxFileSize then
Set streamFile = Server.CreateObject("ADODB.Stream")
streamFile.Type = 1
streamFile.Open
StreamRequest.Position=fileItem.Start
StreamRequest.CopyTo streamFile, fileItem.Length
streamFile.SaveToFile path & fileItem.FileName, 2
streamFile.close
Set streamFile = Nothing
fileItem.Path = path & fileItem.FileName
else
fileItem.FileName = fileItem.FileName & " Discarded - File too big"
end if
Next
End Sub
This method checks if the file in the files collection is larger than the maximum file size and either discards it or saves it.
If discarded it sets the file name to give a message back to the calling routine
**************** end of changes to class file
in the upload page edit the SaveFiles() sub to read
Code:
Set Upload = New FreeASPUpload
Upload.MaxFileSize = 10000
Upload.Save(uploadsDirVar)
It just adds the new line of Upload.MaxFileSize = 10000 where the number is the maximum files size (in bytes)
the example here is 10Kb
also if this is to run on a server where you do not know the physical path to the upload folder use;
Code:
uploadsDirVar = server.mappath("\upload_folder_name")
to set it on the server rather than the hard coded path.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
11-28-2007, 01:52 PM
|
Re: How can I add file size limitations?
|
Posts: 216
|
Thanks so much for your help.
However, I looked in the freeASPupload.asp file for the "private variable declaration block", and I can't find where that is.
And then you instructed to "in the upload page edit the SaveFiles() sub".
Where is the "upload page"?
Thanks again. I look forward to your clarification.
|
|
|
|
11-28-2007, 02:08 PM
|
Re: How can I add file size limitations?
|
Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
|
Quote:
|
I looked in the freeASPupload.asp file for the "private variable declaration block", and I can't find where that is.
|
Where the variables are declared
Code:
Class FreeASPUpload
Public UploadedFiles
Public FormElements
Private VarArrayBinRequest
Private StreamRequest
Private uploadedYet
private m_lMaxFileSize
Quote:
And then you instructed to "in the upload page edit the SaveFiles() sub".
Where is the "upload page"?
|
The page you posted the code for in the first post of this thread
http://www.webmaster-talk.com/asp-fo...tml#post497609
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
11-28-2007, 03:10 PM
|
Re: How can I add file size limitations?
|
Posts: 216
|
Thank you again for your assistance.
Could you please help me again with your instructions "in the upload page edit the SaveFiles() sub to read:
Code:
Set Upload = New FreeASPUpload
Upload.MaxFileSize = 10000
Upload.Save(uploadsDirVar)
I can't find SaveFiles().
Alsom when you say "edit", do you mean replace the code or add the code?
Lastly, when you state =10000, is that KB? Does it need a KB or MB?
Thanks again.
|
|
|
|
11-28-2007, 03:26 PM
|
Re: How can I add file size limitations?
|
Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
|
Your code from first post
Add the line in BLUE
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><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.MaxFileSize = 10000
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>
Quote:
It just adds the new line of Upload.MaxFileSize = 10000 where the number is the maximum files size (in bytes)
the example here is 10Kb
|
the file size is specified in bytes
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
11-28-2007, 03:31 PM
|
Re: How can I add file size limitations?
|
Posts: 216
|
Thank you so much again. I greatly appreciated your help.
Can this be set up so the file size shows in Kbytes instead of bytes?
Thanks again.
|
|
|
|
11-28-2007, 03:52 PM
|
Re: How can I add file size limitations?
|
Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
|
It could;
A extra bit of coding would be needed to the MaxFileSize Property to parse out a multiplier.
Give me 10 - 15 mins 
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
11-28-2007, 04:17 PM
|
Re: How can I add file size limitations?
|
Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
|
new code for the class file
Code:
public function setMaxFileSize(ByVal Val, Mult)
m_sByteMultiplier = Mult
select case lcase(m_sByteMultiplier)
case ""
m_lMaxFileSize = Val
case "k"
m_lMaxFileSize = Val * 1024
case "m"
m_lMaxFileSize = Val * (1024 * 1024)
end select
end function
add this after the MaxFileSize() property definitions.
New variable to go with private m_lMaxFileSize
Code:
private m_sByteMultiplier
and the Upload.MaxFileSize = 10000 needs changing to
Code:
Upload.setMaxFileSize 0.5, "m"
for this the number is the filesize and the string ("m") is the multiplier
so
Upload.setMaxFileSize 0.5, "m" = 1/2 Megabyte
Upload.setMaxFileSize 5, "m" = 5 Megabyte
Upload.setMaxFileSize 10, "k" = 10 Kilobyte
Upload.setMaxFileSize 1000, "" = 1000 Bytes
Took a bit longer but I did make a brew in the middle of it 
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
11-28-2007, 06:26 PM
|
Re: How can I add file size limitations?
|
Posts: 216
|
Thanks again so much for your help.
I looked around the class file, but I would be guessing where exactly to put this code.
Would it be like this (example A):
Code:
Private Sub Class_Terminate()
public property let MaxFileSize(ByVal Val)
m_lMaxFileSize = Val
end property
public property get MaxFileSize()
MaxFileSize = m_lMaxFileSize
end property
public function setMaxFileSize(ByVal Val, Mult)
m_sByteMultiplier = Mult
select case lcase(m_sByteMultiplier)
case ""
m_lMaxFileSize = Val
case "k"
m_lMaxFileSize = Val * 1024
case "m"
m_lMaxFileSize = Val * (1024 * 1024)
end select
end function
Or like this (example B):
Code:
Private Sub Class_Terminate()
public property let MaxFileSize(ByVal Val)
m_lMaxFileSize = Val
end property
public property get MaxFileSize()
MaxFileSize = m_lMaxFileSize
public function setMaxFileSize(ByVal Val, Mult)
m_sByteMultiplier = Mult
select case lcase(m_sByteMultiplier)
case ""
m_lMaxFileSize = Val
case "k"
m_lMaxFileSize = Val * 1024
case "m"
m_lMaxFileSize = Val * (1024 * 1024)
end select
end function
end property
Or neither?
And when you say this "private m_sByteMultiplier" "needs to go with" "private m_lMaxFileSize". What do you mean by "go with"?
And in the upload file, it should now look like this?:
Code:
Set Upload = New FreeASPUpload
Upload.setMaxFileSize 0.5, "m"
Upload.Save(uploadsDirVar)
Thanks again. I look forward to your reply.
Last edited by chrisj; 11-28-2007 at 06:34 PM..
|
|
|
|
11-28-2007, 07:03 PM
|
Re: How can I add file size limitations?
|
Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
|
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
11-28-2007, 07:04 PM
|
Re: How can I add file size limitations?
|
Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
|
Quote:
|
And in the upload file, it should now look like this?:
|
Code:
Set Upload = New FreeASPUpload
Upload.setMaxFileSize 0.5, "m"
Upload.Save(uploadsDirVar)
Yep.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
11-28-2007, 07:29 PM
|
Re: How can I add file size limitations?
|
Posts: 216
|
Thanks again.
So the file should look like this?
Code:
function SaveFiles
Dim Upload, fileName, fileSize, ks, i, fileKey
Set Upload = New FreeASPUpload
Upload.setMaxFileSize 0.5, "m"
Upload.Save(uploadsDirVar)
public function setMaxFileSize(ByVal Val, Mult)
m_sByteMultiplier = Mult
select case lcase(m_sByteMultiplier)
case ""
m_lMaxFileSize = Val
case "k"
m_lMaxFileSize = Val * 1024
case "m"
m_lMaxFileSize = Val * (1024 * 1024)
end select
end function
' If something fails inside the script, but the exception is handled
If Err.Number<>0 then Exit function
And, I'm sorry, I still don't know what to do with:
"New variable to go with private m_lMaxFileSize
Code:
private m_sByteMultiplier
thanks.
|
|
|
|
11-28-2007, 09:02 PM
|
Re: How can I add file size limitations?
|
Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
|
use the class file I posted in the other thread and the only changes to the SaveFiles() sub is the line
Upload.setMaxFileSize 0.5, "m"
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
11-28-2007, 09:54 PM
|
Re: How can I add file size limitations?
|
Posts: 216
|
Thank you again.
One last request regarding this script, if I wanted to re-direct someone after a successful upload, what line of code would I add, to what file would I add it and where in the code, please, would I add it?
Thanks again.
|
|
|
|
11-29-2007, 01:17 PM
|
Re: How can I add file size limitations?
|
Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
|
Code:
else
response.write "<div style=""margin-left:150"">"
OutputForm()
response.write SaveFiles()
response.write "<br><br></div>"
' the redirect would go here
end if
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
|
« Reply to How can I add file size limitations?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|