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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file upload of only specified file types? "HELP!!!"

Status
Not open for further replies.

gavray

Programmer
Jul 17, 2000
65
GB
Hi,

I'm using SA-Fileup to upload files and I only want to only allow certain file types. TXT in my case.

I got this code straight from soft artisans but it does not work. It won't let me upload any file, including txt, which is should allow. Please any ideas

//code

Set upl = Server.CreateObject("SoftArtisans.FileUp")
filename = Mid(Upl.UserFilename,InstrRev

(Upl.UserFilename,"\")+1)

FCONT = upl.ContentType
Response.Write FCONT

FCONT = Mid(FCont, InstrRev(FCont, "/") + 1)

Select Case LCase(FCONT)

Case "txt"
upl.SaveAs "d:\data\fiftyon\emailer\uploadfiles\" & filename & ""

Response.Write &quot;<P>&quot; & FName & &quot; has been saved.&quot; & FCONT



Case else
upl.delete
Response.Write &quot;<font face=verdana size=2>The file you tried to upload is not a text file. This program will only work with text files&quot;

%>
<br><br><a href=&quot;newsletter.asp&quot;><font size=3>Re-try with a text file</font></a>
<%
Response.End
End Select

 
Do you try this?

Resource located at:


Restricting File Types
There are times when you may need to restrict the type of files uploaded from a client's machine. You can do so by using the ContentType property of SA-FileUp and a Select condition to save only files that are a certain type.

Once your file is uploaded, you would include the following line after you have created an instance of SA-FileUp:

<%
....
'--- Parses out the file name
'---
FName = Mid(upl.UserFilename, InstrRev(upl.UserFilename, &quot;\&quot;) + 1)

'--- This line parses out the Content type.
'---
FCONT = upl.ContentType

'--- This would equal to
'--- &quot;image/pjpeg&quot;
'--- &quot;image/gif&quot;
'--- &quot;text/plain&quot;, etc.

'--- You can then use the Select Case Condition to restrict the file type.
Select Case LCase(FCONT)
Case &quot;image/gif&quot;
upl.Save
Response.Write &quot;<P>&quot; & FName & &quot; has been saved.&quot;

Case &quot;image/pjpeg&quot;
upl.Save
Response.Write &quot;<P>&quot; & FName & &quot; has been saved.&quot;

Case Else
upl.delete
Response.Write &quot;<P>&quot; & &quot;You are restricted to only upload gif and Jpeg files.<BR>&quot;
Response.End
End Select

....
%>

Let go though the code:

First we parse out the filename using two handy methods - Mid() and InstrRev(). The combination of these methods and the UserFilename property should get us just the name of the file, not the full path. The variable Fname is used within the response of the Case condition statement.


We get the media content type of the file uploaded by using the ContentType property. In this example, we restrict all file except Gif and Jpeg files. So we are looking for either &quot;image/pjpeg&quot; or &quot;image/gif&quot; as the media content type.


A Case condition statement is used to save the correct file and send a successful response. Notice that the variable FName is used to supply the file that was saved. If the file type is incorrect, the file will be deleted and a error response will given.


--------------------------------------------------------------------------------
What did we learn?

Now you can restrict file types with ease.

To identify a file's name, use the Mid() and InstrRev() methods with the UserFilename property.
To restrict a file by media type, identify the media content of an uploaded file using the ContentType property.
To acknowledge the sucessful completion of the file restriction, use a Case condition statement.
That completes the Basics tutorial. You can also try the other tutorials:

Transactional Processing
Common Client-Side Solutions





Hope this helps

Wayne Sellars
 
Funnily enough If you want to restict to just text then use
&quot;plain&quot; as the definition. Rather than &quot;text/plain&quot;

Gav
 
Hi there!

Is there a way to restrict file types when the browse common dialog window appears? I'm using my own upload engine and the form Enc type is Multipart

Thanks

Vercesi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top