Click to See Complete Forum and Search --> : ASPUpload Help


thejbsmith
02-08-2007, 04:20 PM
I'm trying to upload files using ASPUpload. In the form the user has to select a folder to upload to, and then add the file. My problem is that I cannot get it to work where ASPUpload will upload to the selected folder. Since folders can be added or deleted at anytime, I need this to be scripted in, but with ASPUpload you cannot get information from the multipart form until after the .Save function is called. I am using godaddy.com for the hosting, which does not allow you to use the .Save function of ASPUpload, you must use the .SaveVirtual function.

This is the line I am using to save the file, but it will not get the value from the form so Upload.Form is empty. I can take off the last / and have it upload, but not to the selected folder.

Count = Upload.SaveVirtual("files/" & Upload.Form("category") & "/")

Any help would be greatly appreciated.

lmf232s
02-08-2007, 06:17 PM
You might want to add this to your page and verify that your trying to upload to a valid directory. Upload.SaveVirtual is the same as Server.MapPath.

Response.write Server.MapPath("files/" & Upload.Form("category") & "/")

thejbsmith
02-08-2007, 08:43 PM
I added that, and I'm still not being able to upload to the specific folder. This is what I have right now:

Count = Upload.SaveVirtual("files/" & Upload.Form("category") & "")

Response.write (Server.MapPath("files/" & Upload.Form("category") & "/"))


In the Upload.SaveVirtual line, the Upload.Form("category") does not return anything. It only uploads to the files folder. There should be a / in the last set up parenthesis, but since it doesn't return anything from the form it just makes the upload directory .../files// and returns an error bc of the extra /.

The Response.Write line displays correctly, except that it does not write the final /. I'm not sure if this is because the form request messes it up or not.

So...the main problem is that nothing is returned in the SaveVirtual line from the form, only after that.

mrizwan
02-09-2007, 09:45 AM
My suggestion is to upload files to some fixed temp folder first. Then after uploading is done move the file to appropriate folder.

To move file this function will be helpful.
UploadForm.MoveFile (FromPath As String, ToPath As String.

thejbsmith
02-09-2007, 03:36 PM
Thanks for the help. I got everything working right, even if it is just going around some of the problems ASPUpload has.

In case anybody else has this problem, here's the code that ended up working:

<%
'Declare variables
Dim SQL

Set Upload = Server.CreateObject("Persits.Upload")

Upload.OverwriteFiles = False
'On Error Resume Next

Upload.SetMaxSize 1048576 ' Limit files to 1MB
Count = Upload.SaveVirtual("files/" & Upload.Form("category"))

Dim FromPath, ToPath, dbPath

For Each File in Upload.Files

'check to make sure a file has been selected
If File.FileName = "" Then
File.Delete
Session("error") = "Please select a file"
Response.Redirect("fileAdmin.asp?task=add")
End If

'check to see if a category has been selected
If Upload.Form("category") = "" Then
File.Delete
Session("error") = "Please select a category"
Response.Redirect("fileAdmin.asp?task=add")
End If

'declare variables and set them equal to the upload path(FromPath) and where I want them to go(ToPath)
'also declare variable for path to be stored in database to retrieve file later
FromPath = CStr(File.Path)
ToPath = CStr(Server.MapPath("files") & "\" & Upload.Form("category") & "\" & File.FileName)

dbPath = CStr("http://www.mysite.com/files/" & Upload.Form("category") & "/" & File.FileName)

'check to see if there is already a file in that location
SQL = "SELECT * FROM Files WHERE FileLoc='" & dbPath & "';"
Set oRS = oConn.Execute(SQL)

'if there is no file in that location moves file there and adds location to db
If oRS.EOF Then
'move file from upload path and insert values into DB for later retrieval
Upload.MoveFile FromPath,ToPath
SQL = "INSERT INTO Files(FileID, CategoryID, FileLoc) VALUES ('" & File.FileName & "', '" & Upload.Form("category") & "', '" & dbPath & "');"
Call oConn.Execute(SQL)
Session("error") = "File uploaded Successfully"
'if there is a file there already, then delete uploaded file and set error message
Else
'delete file bc it already exists
File.Delete
Session("error") = "File already exists"
End If

Next

'redirects to upload page
Response.Redirect("fileAdmin.asp?task=add")
%>

With this solution, in order to get the files into the specific directory, you have ASPUpload move the files there. If there is already a file there with the same name, then there will be an error, hence checking against the values in the database

mrizwan
02-09-2007, 04:02 PM
With this solution, in order to get the files into the specific directory, you have ASPUpload move the files there. If there is already a file there with the same name, then there will be an error, hence checking against the values in the database
I think this is not a good way to disallow a file to be uploaded for the reason that "file already exists". Instead of this, if file is already there, you can rename files according to some algorithm, so that all the files are uploaded and you don't need to chk and manage database for this purpose aswell.

Like you can use following code for checking if a files exists or not. And if exists change the file name to something that doesn't exists.

<%
dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
sFilePath = "c:\asp\introduction.asp"
iCounter = 0
do while fs.FileExists(sFilePath)=true
sFilePath = left(sFilePath, len(sFilePath)-4) & iCounter & right(sFilePath, 4)
iCounter = iCounter + 1
loop
set fs=nothing
%>

I hope this helps.