Monday, May 26, 2008

Upload Files

//Form tag in upload page



Enctype
The mime type to be used for encoding the form data.
Some examples are:
enctype="text/plain" data is sent as plain text
enctype="multipart/form-data" used when files are uploaded

//To browse a file, give Type=”FILE”
Example:

Sample Code:
Upload code differs for different components used. I am giviing below the code for "Persits" component which i have used.

//Create Upload object
Set Upload=Server.CreateObject("Persits.Upload.1")

//If a file already exists with same name then we can give option whether to upload or not to upload.
Upload.OverwriteFiles = False/True
Eg:
True:
If swetha.jpg already exists then also the file is uploaded as swetha[1].jpg
False:
If same name already exists then we cannot upload.

//If there is any error in uploading and we still want to move ahead then we give this. If we want to see the error then we should remove this statement.
On Error Resume Next

//Set File Limit to upload
Upload.SetMaxSize 5242880

//We can also add code for creating the folder if the specified does not exist
Set fso=Server.CreateObject("Scripting.FileSystemObject")
if fso.FolderExists(server.mappath("./CoachImages"))=false then
Set fobj=fso.CreateFolder(server.mappath("./CoachImages"))
end if
Set fobj=nothing
Set fso=nothing

//To get the count of the uploaded files
Count = Upload.Save(server.mappath("./"&folder))

//To know if a file a uploaded or not. If not Show Error Message
if Count = 0 then
Error

// In Else part, we increment a loop for the uploaded Files
For Each File in Upload.Files


//Here we check the type of file. Suppose it is an image, then
If ucase(File.ImageType) = "GIF" or ucase(File.ImageType) = "JPG" …….then
//To get File Name
File.FileName
//To get Image Width
File.ImageWidth
//To get Image Height
File.ImageHeight
Else //If file type does not match then we can delete the file
File.Delete
End If

Next

//To delete an old uploaded file
Set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FileExists(Server.MapPath("./BoardImages/"&IRs("photoname"))) then
fs.DeleteFile(Server.MapPath("./BoardImages/"&IRs("photoname")))
end if
set fs=nothing

After clicking SUBMIT, in Javascript
function SendBoardValues()
{
window.close();
window.opener.evntfrm.submit();
}
Here, opener is used to get the values in the current window to the main page in the browser.

No comments: