Limiting the Upload Size


You've seen how to upload multiple files and access each of their individual properties, as well as how to read properties of the file, such as the TotalBytesof the upload.

There are several properties that can be set as well. One very useful property is MaxBytes. MaxBytesallows you to set a firm limit on the size of the uploaded file, ensuring that malicious users do not fill up your server's hard disk with huge files.

When you set the MaxBytes property, FileUpwill write up to the number of bytes you specify and then stop. Any additional data will be discarded. You can set MaxBytes once and it will apply to all files in the current upload, limiting each of them to the value that you specify.

The default value of the MaxBytes property is zero (0). fileUpload.MaxBytes = 0 When MaxBytes is set to its default value of zero, there is no limit on the size of files that can be uploaded.

Set MaxBytesbefore referencing any form elements.

As soon as a form element is referenced, FileUp will cache the upload on the server. If you set MaxBytes after referencing a form element, by the time the MaxBytes property is set, the upload will have been cached. Note: if you set MaxBytes after referencing a form element, but before saving the uploaded files, MaxBytes will take effect when you save.

In the following example, we use the same simple form used in the first upload :

The Client Code
<html>

<head>
<title>Please Upload Your File</title>
</head>

<body>

<form enctype="multipart/form-data" method="post"
action="formresp.asp">

Enter filename to upload:
<input type="file" id="myFile" name="myFile" /><br />

A file larger than 1000 bytes will be truncated on the server.<br />
<input type="submit" />

</form>
</body>
</html>

Here is the response file, formresp.asp:

The Server Code
<%@ language="vbscript" %>
<html>

<head>
<title>Upload File Results</title>
</head>

<body>
<%
Dim fileUpload
Set fileUpload = Server.CreateObject("SoftArtisans.FileUp")

fileUpload.Path = "C:\Temp"
fileUpload.MaxBytes = 1000

'--- Check to be sure there was a file selected in the form
'--- If so, continue processing
If IsObject(fileUpload.Form("myFile")) Then
If Not fileUpload.Form("myFile").IsEmpty Then

fileUpload.Form("myFile").Save

Response.Write "FileUp Saved the File Successfully." & "<br />
Response.Write "Total Bytes sent by the browser: " & Request.TotalBytes & _
" <br /> "
Response.Write "Total Bytes Saved: " & _
fileUpload.Form("myFile").TotalBytes & "<br />"
Else
Response.Write "There was no file submitted in the form field."
End If
Else
Response.Write "The referenced field does not exist " & _
"or is not of type=""file"""
End If

'--- Dereference FileUp
Set fileUpload = Nothing
%>
</body>
</html>

In the upload processing script (formresp.asp), MaxBytes is set to 1000. That means that FileUp will save up to 1000 bytes of each uploaded file. If you try to upload a file larger than 1000 bytes, notice that only the first 1000 bytes would be written to the hard disk. No error will be raised, indicating that the file was truncated.

MaxBytes is Read/Write property, meaning that it is possible to both set its value and retrieve its value.

Because the MaxBytes setting truncates the file without raising an error, the server code is set up to display the number of bytes sent by the browser in comparison to the number of bytes actually saved on the server. This is a comparison between Request.TotalBytes and FileUp's TotalBytes property.

You may have noticed that the total number of bytes transmitted by you is larger than your original file's size on the client file system. This is normal, since the browser must add information such as headers and encoding information. Request.TotalBytesreports the total including the file, encoding information, and other form elements that may be present.


Summary

See also, MaxBytesToCancel.

Top

Copyright © 2010 SoftArtisans, Inc. All rights reserved.