Uploading certain file types only or
disallow from AspUpload and Persits.Upload to upload file extension.
Originally Asp file Upload can do many useful things like limiting file size or total upload size using, where and how to save the file an other many things you will use regularly upon Persits file Upload.
but limiting file type is not built-in under Persits object so you may need to do it yourself.
First way using SaveToMemory:
First you need to use SaveToMemory instead of Save.
this way the file is not saved until you use the SaveAs Method.
<%
sAllow = ".doc|.txt|.png|.jpg|.gif|.pdf|.zip"
Set Upload = Server.CreateObject("Persits.Upload")
Upload.SaveToMemory
For Each File in Upload.Files
Ext = Lcase(Right(strFileName, 3))
strFileName = Lcase(File.FileName)
If instr(sAllow, Ext) Then
File.SaveAs "D:\home\www\uplodfiles\" & strFileName
Else
Response.Write "File: " & strFileName & " disallow file type"
End If
Next
%>
Second way if you prefer to use Save Method:
In this way the file is first saved, then your code decide if Deleted the file
or to leave on the server.
<%
sAllow = ".doc|.txt|.png|.jpg|.gif|.pdf|.zip"
Set Upload = Server.CreateObject("Persits.Upload")
Upload.Save "D:\home\www\uplodfiles\"
For Each File in Upload.Files
Ext = Lcase(Right(strFileName, 3))
strFileName = Lcase(File.FileName)
If instr(sAllow, Ext) Then
File.Delete
Response.Write "File: " & strFileName & " disallow file type"
End If
Next
%>
האובייקט הוותיק להעלאת קבצים מטפל באופן מובנה בהרבה מאפיינים חשובים, גדלי קבצים, מיקום שמירה ועוד
תכונות חשובות. בנוגע לסוגי קבצים הטיפול נמצא בידי המתכנת. איך עושים זאת?
איך מונעים מאוביקט AspUpload מלקבל סוגי קבצים לא רצויים:
ובכן שיטה אחת מבצעים את תהליך השמירה לזכרון בעזרת SaveToMemory
במקום Save. ובעבור כל קובץ מחליטים האם לשמור אותו או לא.
בשיטה השנייה שומרים את הקובץ באופן רגיל בעזרת Save.
אם הקובץ לא רצוי מפעילים את Deleted.
|