Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 8th, 2004, 12:00 PM
Authorized User
Join Date: Jan 2004
Location: , , .
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

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)))
' 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")
    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)
            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"
        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))


        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
            end if

        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
        objConn.execute strSQL

    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")
       response.redirect "../nologon.asp?mid=5"
    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
                ' Regular field
                uploadRequest.add getFieldName(fieldInfo), fieldValue
            End If
        End If
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
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter File Types in ASP.NET File Upload ramuis78 ASP.NET 2.0 Basics 2 May 31st, 2007 10:50 AM
To upload an Excel File to the Server using ASP SMM Classic ASP Professional 4 March 26th, 2007 03:49 AM
File Upload in ASP qazi_nomi Classic ASP Basics 1 December 11th, 2004 10:14 AM
Please Help: ASP file upload and download vtcyee Classic ASP Components 1 July 10th, 2004 06:21 AM
About file upload and download using ASP vtcyee Classic ASP Databases 1 July 10th, 2004 12:27 AM

All times are GMT -4. The time now is 07:59 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.