Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check File Size before saving to server

Status
Not open for further replies.

TechnicalAnalysis

Programmer
Jul 2, 2001
169
US
Is there a way to check the file size before saving it to t the server?
I know how to check the file size on the server. I want to check the file size before putting it on the server during the File Upload process on the web page.
Any suggestion is appreciated.

Regards
 
You could use the size property of VBScript's FileSystemObject objectm which will display the total number in bytes of the file.

<%
Dim filesys, demofile, createdate
Set filesys = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set demofile = filesys.GetFile(&quot;c:\inetpub\ ' change this to reflect the physical path to your file

Response.Write(&quot;The file &quot; & demofile & &quot;is &quot; & demofile.Size & &quot; bytes&quot;)
%>

You could then employ the Save method or the CreateTextFile method of FileSystemObject to either save or overwrite the text file, respectively.


HTH,
Jason
 
U could use Request.TotalBytes property to see the aproximate size of the client data sended to the server ________
George, M
 
if you want to not just check the size before saving, but limit the size uploaded, there's a MaxBytes property (at least with SAFileUp) that will stop the file upload at the file size MaxBytes is set to, then you can check to see if TotalBytes = MaxBytes, if so, then don't save the file.

if you're using SAFileUp and want the exact code to do that, i can post it for you.
 
...or you could just combine any of the above with a simple IF...THEN construct to determine whether the file gets saved to the server, or not.

EX:

<%
Dim filesys, demofile, createdate
Set filesys = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set demofile = filesys.GetFile(&quot;c:\inetpub\ ' change this to reflect the physical path to your file

If demoFile.Size > 1024 Then
' do something to save it to the server
Response.Write(&quot;Your file was saved!&quot;)
Else
Response.Write(&quot;Your file is too large to be saved!&quot;)
%>


HTH,
Jason
 
lobstah

I'm interested in SAFileUp. Any chance that you can point me to the right material or post some code here.

Thanks
 
Note: You must have SAFileUp from SoftArtisans installed on your server for this to work. This is not a free component.

input form:

<form name=&quot;Upload&quot; enctype=&quot;multipart/form-data&quot; action=&quot;upload.asp&quot; method=&quot;post&quot;>

<table align=center width=600>
<tr><td>File:</td><td><input type=&quot;file&quot; name=&quot;file_name&quot; size=&quot;30&quot;></td></tr>
<tr><td><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;>&</td></tr>
</table>
</form>


portion of upload.asp:

Dim oUpload
Dim sMaxFileSize
Dim sFileName

' Maximum file size allowed - zero defaults to no limit, otherwise specify in bytes
CONST UPL_MAX_FILE_SIZE = 1000000

Set oUpload = Server.CreateObject(&quot;SoftArtisans.FileUp&quot;)
oUpload.MaxBytes = UPL_MAX_FILE_SIZE

' check for file selected for upload
If Not oUpload.IsEmpty Then
'check for file exceeding max allowable size
If oUpload.TotalBytes = oUpload.MaxBytes Then
'file exceeds max allowed file size
sError = &quot;File size exceeds maximum allowable file size of &quot; & oUpload.MaxBytes
Else
'file size okay, continue
sFileName = Mid(oUpload.UserFilename, InstrRev(oUpload.UserFilename, &quot;\&quot;) + 1)
oUpload.SaveAs fullpath & sFileName 'where fullpath = path to server directory where upload files stored
End If
Else
'return error string for no file selected
sError = &quot;You must select a File.&quot;
End If

If sError = &quot;&quot; Then
Set oUpload = Nothing
Else
Response.Write sError
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top