Click to See Complete Forum and Search --> : Upload to different directories


jrthor2
06-06-2003, 10:53 AM
I have a form that has 4 input boxes for people to enter files to be uploaded. I want to have it so that if you upload a file with the extension of .jpg, .gif, .tif, .eps, it goes to an images directory, and anything else goes to a files directory. The input boxes are named attach1, attach2, attach3, attach4. How would I set up an if statement to do this?

Thanks

cmelnick
06-06-2003, 11:55 AM
It depends slightly on which Upload package you are using. Whichever one you use SHOULD have access to the file name that is being uploaded. See the following code for how to find the extension.


' Assume that the filename is in txtFilename variable
txtExt = LCase(Right(txtFilename, 3)
dir = "C:\Inetpub\upload\"

If txtExt = "jpg" Or _
txtExt = "bmp" Or _
txtExt = "gif" Then

dir = dir & "images\"

Else

dir = dir & "documents\"

EndIf

' Then whichever upload component you use should
' have a method to save the uploaded file,
' and you would use dir & txtFilename for the path name

Let me know if you need help with finding/using an upload component.

jrthor2
06-06-2003, 12:07 PM
how would I get that to work if I have 4 upload boxes, and not just 1?

cmelnick
06-06-2003, 12:34 PM
Attached is the upload script that I use.

upload.asp is the upload form
inc/upload.asp is the actual upload component.

Sorry about the duplicate name. My brain isn't working well enough to be creative today.

Anyway, I fixed the upload to upload and parse several files, but I didn't test it, so you might have to debug it, but in theory, it should work. Enjoy!

jrthor2
06-06-2003, 01:07 PM
That's not working at all. It says the file was uploaded, but it is not there??

cmelnick
06-09-2003, 12:09 PM
In upload.asp (not inc/upload.asp) change
File.SaveToDisk Server.MapPath(extDir & "/" File.Filename)
to
File.SaveToDisk Server.MapPath(extDir)

jrthor2
06-09-2003, 12:41 PM
thanks, that works fine now

jrthor2
06-10-2003, 09:17 AM
I was wondering if I could set this up to send me an email of the list of files that were uploaded. I have the mailer working, but it only shows me the path of the last file that was uploaded (which makes sense), but how could I get it to list the files uploaded. Below is my code:

<%@ Language=VBScript %>
<%
option explicit
Response.Expires = -1
Server.ScriptTimeout = 600
%>
<!-- #include file="inc/upload.asp" -->
<%
Dim mesg, Uploader, File, baseDir, extDir, txtExt, bodytag, Mailer
Set Uploader = New FileUploader

baseDir = "/upload"

Uploader.Upload()

If Uploader.Form("submit") = "Upload" then
If Uploader.Files.Count = 0 Then
mesg = "File Not Uploaded"
Else
For Each File In Uploader.Files.Items
txtExt = LCase(Right(File.Filename, 3))

If txtExt = "jpg" Or _
txtExt = "bmp" Or _
txtExt = "tif" Or _
txtExt = "eps" Or _
txtExt = "png" Or _
txtExt = "gif" Then

extDir = baseDir & "/images"

Else

extDir = baseDir & "/files"

End If
File.SaveToDisk Server.MapPath(extDir)
Next
mesg = "File uploaded successfully"

Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Mailer.FromName = "Zion Upload Files"
Mailer.FromAddress = "someone@someplace.com"
Mailer.RemoteHost = "localhost"
Mailer.AddRecipient "Test", "someone@someplace.com"
Mailer.Subject = "Files have been uploaded to Zion's website"
Mailer.BodyText = "The following files have been uploaded to the upload directory on Zion's website:" & Chr(13) & Chr(13) & extDir
if Mailer.SendMail then
else
Response.Write "Mail send failure1. Error was " & Mailer.Response
end if

End If
End If
%>

cmelnick
06-11-2003, 09:33 AM
You could just create a variable which appends each filename as they are being saved to the disk.

<%
Dim txtFileList

For Each File In Uploader.Files.Items
' Your normal file processing here

txtFileList = txtFileList & "images/" & File.FileName & ", "
' or
txtFileList = txtFileList & "documents/" & File.FileName & ", "
Next

%>

Then you can include that in your e-mail.
"The following files have been uploaded: " & txtFileList

jrthor2
06-11-2003, 09:39 AM
Ok, this is what I have for my code:

For Each File In Uploader.Files.Items
txtExt = LCase(Right(File.Filename, 3))

If txtExt = "jpg" Or _
txtExt = "bmp" Or _
txtExt = "tif" Or _
txtExt = "eps" Or _
txtExt = "png" Or _
txtExt = "gif" Then

extDir = baseDir & "/images"

Else

extDir = baseDir & "/files"

End If
File.SaveToDisk Server.MapPath(extDir)
txtFileList = txtFileList & "images/" & File.FileName & ", "
' or
txtFileList = txtFileList & "files/" & File.FileName & ", "

Next
mesg = "File(s) uploaded successfully"

Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Mailer.FromName = "Zion Upload Files"
Mailer.FromAddress = "webmaster@zluth.org"
Mailer.RemoteHost = "localhost"
Mailer.AddRecipient "Jason Roscoe", "webmaster@zluth.org"
Mailer.Subject = "Files have been uploaded to Zion's website"
Mailer.BodyText = "The following files have been uploaded to the upload directory on Zion's website:" & Chr(13) & Chr(13) & txtFileList & Chr(13)

And in my email, If I only upload an image, I get this:

The following files have been uploaded to the upload directory on Zion's website:

images/capitol001.jpg, files/capitol001.jpg,

But it has only been uploaded to the images directory, but the email says it has been uploaded to both directories.

cmelnick
06-11-2003, 09:52 AM
See the code in blue

Originally posted by jrthor2
So if I hve this:
Dim txtFileList

For Each File In Uploader.Files.Items
txtExt = LCase(Right(File.Filename, 3))

If txtExt = "jpg" Or _
txtExt = "bmp" Or _
txtExt = "tif" Or _
txtExt = "eps" Or _
txtExt = "png" Or _
txtExt = "gif" Then

extDir = baseDir & "/images"
txtFileList = txtFileList & "images/" & File.FileName & ", "

Else

extDir = baseDir & "/files"
txtFileList = txtFileList & "files/" & File.FileName & ", "

End If
File.SaveToDisk Server.MapPath(extDir)
Next
mesg = "File(s) uploaded successfully"


Where would your code go??

jrthor2
06-11-2003, 10:00 AM
got it, thanks

cmelnick
06-11-2003, 05:04 PM
Glad to be able to help...