File upload in ASP
I need to upload a file to a network shared drive. I have created the path to the folder in IIS under my application.
The following code is supposed to save files to that folder directly. I keep getting the 'Path not found' message, even with all permissions set on the directory in question. Please tell me what is wrong. Thanks.
<%Option Explicit
dim path, Conn
path="../"
if session("loggedin")= "" then
response.redirect "../nologon.asp?mid=2"
end if
session("where") = "Projects"
session("mypart") = "Narratives"
' This value may need increasing if dealing with large files
Server.ScriptTimeout = 5400
Dim postedData, binData, counter, contentType, errorMsg
Dim boundary, formData, uploadRequest
Dim fso, browserType, startPos, endPos
Dim filePath, fileName, savePath, savefile, fileCount
dim strSQL, objConn, fileType, fileSize
Dim requestFiles(9, 1) ' Allow for ten files
Dim oFSO
Dim sMyFile, sFolder
dim objFile,objFolderContents, objFolder, objFileItem
'create database connection
set objConn=Server.createobject("adodb.connection")
'open connection
Conn="DRIVER={SQL Server};SERVER={server};DSN=mydsn;DATABASE={dbase} ;"
objConn.Open Conn
' Read all the form data as binary
binData = Request.BinaryRead(Request.TotalBytes)
' Convert the binary data to ASCII
For counter = 1 To LenB(binData)
postedData = postedData & Chr(AscB(MidB(binData, counter, 1)))
Next
' The Request.Form data is no longer available after a BinaryRead
' so we'll need to manually parse the Form variables
' Find the encoding type
contentType = Request.ServerVariables("HTTP_CONTENT_TYPE")
' Ensure the Form's encoded for multipart/form-data
If InStr(contentType, "multipart/form-data") > 0 Then
' Get the boundary of the content type
endPos = InStrRev(contentType, "=")
boundary = Trim(Right(contentType, Len(contentType) - endPos))
' Get the form data from the posted data, using the boundary
formData = Split(postedData, boundary)
'Extract the information for each variable and its data
Set uploadRequest = CreateObject("Scripting.Dictionary")
parseFormData
Else
errorMsg = "Incorrect encoding type"
End If
' Save the posted file(s)
Set fso = server.createObject("Scripting.FileSystemObject")
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
browserType = UCase(Request.ServerVariables("HTTP_USER_AGENT"))
If uploadRequest.Item("submit") = "Upload" then
For counter = 0 To FileCount - 1
' Strip the path info out if not a MAC
If (InStr(browserType, "WIN") > 0) Then
startPos = InStrRev(requestFiles(counter, 1), "\")
fileName = Mid(requestFiles(counter, 1), startPos + 1)
ElseIf (InStr(browserType, "MAC") > 0) Then
fileName = requestFiles(counter, 1)
Else
startPos = InStrRev(requestFiles(counter, 1), "/")
fileName = Mid(requestFiles(counter, 1), startPos + 1)
End If
'physical path to the file name
sMyFile = Server.MapPath("/SERVER/action_documents/" & fileName)
'if file really exists then tell user
If oFSO.FileExists (sMyFile) Then
response.redirect "../nologon.asp?mid=4"
response.end
End If
' Set the path on the Server
' Save the file
'myfolder = fso.GETFolder(Server.MapPath("uploadfoler"))
savePath = Server.MapPath("../SERVER/action_documents/" & fileName)
Set saveFile = fso.CreateTextFile(savePath, True)
saveFile.Write(requestFiles(counter, 0))
saveFile.Close
fileType=right(fileName,3)
set objFile=fso.GetFile(savePath)
set objFolder = objFile.ParentFolder
set objFoldercontents = objFolder.files
for each objFileItem in objFolderContents
'response.write objFileItem.Size & "<BR>"
if fileName=objFileItem.Name then
fileSize=objFileItem.Size
end if
next
strSQL="INSERT INTO action_documents(action_id,document_name, document_filename, document_type, document_size, date_uploaded)"&_
" VALUES(" & uploadRequest.Item("action_id") & ",'" & uploadRequest.Item("document_name") & "','" & fileName & "','" &fileType & "'," & fileSize & ",'" & FormatDateTime(now(),vbLongDate) & "')"
'response.write strSQL
'response.end
objConn.execute strSQL
Next
If errorMsg = "" Then
response.redirect "../actions/edit_action.asp?project_id=" & uploadRequest.Item("project_id") & "&phase_id=" & uploadRequest.Item("phase_id") & "&action_id=" & uploadRequest.Item("action_id")
Else
response.redirect "../nologon.asp?mid=5"
Response.end
End If
' Clear up afterwards
Set saveFile = Nothing
Set fso = Nothing
Set uploadRequest = Nothing
' Procedure to parse the Form data
Private Sub parseFormData()
Dim counter, endMarker, fieldInfo, fieldValue
For counter = 0 To UBound(formData)
endMarker = InStr(formData(counter), vbCrLf & vbCrLf)
If endMarker > 0 Then
' Get the information for this field
fieldInfo = Mid(formData(counter), 3, endMarker - 3)
' Get the value for this field
fieldValue = Mid(formData(counter), endMarker + 4, _
Len(formData(counter)) - endMarker - 7)
' Check to make sure this a file
If (InStr(fieldInfo, "filename=") > 0) Then
' Store the value and filename in our array
requestFiles(fileCount, 0) = fieldValue
requestFiles(fileCount, 1) = getFileName(fieldInfo)
' Check the file was provided
If requestFiles(fileCount, 1) <> "" Then
fileCount = fileCount + 1
End If
Else
' Regular field
uploadRequest.add getFieldName(fieldInfo), fieldValue
End If
End If
Next
End Sub
' Function to return the field name
Private Function getFieldName(ByVal strFileName)
Dim startPos, endPos, strQuote
strQuote = Chr(34)
startPos = InStr(strFileName, "name=")
endPos = InStr(startPos + 6, strFileName, strQuote & ";")
If endPos = 0 Then
endPos = inStr(startPos + 6, strFileName, strQuote)
End If
getFieldName = Mid(strFileName, startPos + 6, endPos - (startPos + 6))
End Function
' Function to return a filename
Private Function getFileName(ByVal strFileName)
Dim startPos, endPos, strQuote
strQuote = Chr(34)
startPos = InStr(strFileName, "filename=")
EndPos = InStr(strFileName, strQuote & vbCrLf)
getFileName = Mid(strFileName, startPos + 10, endPos - (startPos + 10))
End Function
%>
|