Results 1 to 6 of 6

Thread: How to upload a file from client site to server

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2001
    Location
    mumbai
    Posts
    12

    Cool How to upload a file from client site to server

    I don't know how to upload any file from client site to server and setting the file entry in data base. If anybody knows please tell me how to code it

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Canada
    Posts
    264
    You need a third party component.

    you can use one of the free ones.

    http://www.aspsmart.com

    look at their aspsmartupload

    It comes with examples and everything.
    In the beginning the universe was created. This has made a lot of people very angry and is generally regarded as a bad idea.

    - Douglas Adams
    The Hitchhiker's Guide to the Galaxy

  3. #3
    Lively Member
    Join Date
    May 2001
    Location
    Falkenberg, Sweden
    Posts
    76
    Actually you can do this wihtout an component, I've done it many times and it works excellent! You'll find VBScript code for this here:
    http://www.asptoday.com/content/arti...CaTlg27dSP4cCa

    This is very useful, when many webhosting-services does not have any upload-component.
    ________________________
    Fredrik Klarqvist

  4. #4
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    You need a subscription to view that article...
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  5. #5
    Member
    Join Date
    Oct 2000
    Location
    Netherlands
    Posts
    54

    Need subscription

    Not everybody has a subscription to this site, anyone has this code, or a look-a-like in its possesion?
    A mind is like a parachute, it has to open to let it work
    www.2beesoft.com for Icon Manager with over 20.000 free icons
    VB6 Ent. SP4, ASP, W2000/W98

  6. #6
    Lively Member
    Join Date
    May 2001
    Location
    Falkenberg, Sweden
    Posts
    76
    Hi again, yes I know that you must be a subscriber, but I thought that maybe some of you was, but ok...I've built an VBscript class to be used from ASP (which means that it must be run under win2k with IIS5).
    Code:
    <%
    '###################################### NEW CLASS #############################################
    class clsUploadFile
    private byteCount
    private binRead
    
    
    private Function getByteString(StringStr)
    dim i
    dim char
     For i = 1 to Len(StringStr)
     	char = Mid(StringStr,i,1)
    	getByteString = getByteString & chrB(AscB(char))
     Next
    End Function
    '-----------------------------------------------------------------
    private Function getString(StringBin)
    dim intCount
     getString =""
     For intCount = 1 to LenB(StringBin)
    	getString = getString & chr(AscB(MidB(StringBin,intCount,1))) 
     Next
    End Function
    '-----------------------------------------------------------------
    public function getValFromBin(compStr, delimiter)
    	dim namePosStart
    	dim namePosEnd
    	dim nameByteLen
    	dim strTmp 'as string
    	dim blnUseFix 'as boolean
    	
    	blnUseFix = false
    
    	if delimiter= "" then 
    		delimiter = "-" 'chr(10)	
    		blnUseFix = true	
    	end if	
    
    	namePosStart = InstrB(1,binRead,getByteString(compStr)) + len(compStr) + 1
    	namePosEnd = InstrB(namePosStart,binRead,getByteString(delimiter))
    	nameByteLen = namePosEnd - namePosStart
    	strTmp = getString(MidB(binRead,namePosStart,nameByteLen))
    	if blnUseFix = true then 	
    		'special fix to read from post
    		strTmp = replace(strTmp, chr(13), "|")
    		strTmp = replace(left(strTmp, 3), "|", "") &right(strTmp, len(strTmp)-3)
    		strTmp = left(strTmp, instr(strTmp, "|")-1)	
    		strTmp = replace(strTmp, chr(10), "")	
    	end if
    	
    	getValFromBin = strTmp
    end function
    '-----------------------------------------------------------------
    public sub initialize()
    	byteCount = Request.TotalBytes
    	binRead = Request.BinaryRead(byteCount)
    end sub
    '-----------------------------------------------------------------
    public function strGetObjName()
    	strGetObjName = getValFromBin ("name=", chr(34))
    end function
    '-----------------------------------------------------------------
    public function strGetFileName()
    	strGetFileName = getValFromBin("filename=", chr(34))
    	strGetFileName = mid(strGetFileName,InStrRev(strGetFileName, "\") + 1, len(strGetFileName))
    end function
    '-----------------------------------------------------------------
    public function strGetContentType()
    	strGetContentType = getValFromBin("Content-Type:", chr(13))
    end function
    '-----------------------------------------------------------------
    public sub saveFile(strPath, strFile)
    	dim boundary
    	dim posBeg
    	dim posEnd
    	dim objValue
    	dim Value
    	dim scriptobject
    	dim myfile
    	dim i
    	
    	boundary = MidB(binRead,1,InstrB(1,binRead,getByteString(chr(13)))-1)
    	PosBeg = InstrB(1,binRead,getByteString("Content-Type:")) + 14
    	PosEnd = InstrB(PosBeg,binRead,getByteString(chr(13)))
    	PosBeg = PosEnd + 4
    	PosEnd = InstrB(PosBeg,binRead,boundary)-2
    	objValue = MidB(binRead,PosBeg,PosEnd-PosBeg)
    
    	value = objValue
    	Set ScriptObject = Server.CreateObject("Scripting.FileSystemObject")
    	call checkFolderAndCreate(strPath)	
    	Set MyFile = ScriptObject.CreateTextFile(strPath &strFile)
    	For i = 1 to LenB(value)
    		MyFile.Write chr(AscB(MidB(value,i,1)))
    	Next
    	 
    	MyFile.Close
    end sub
    '-----------------------------------------------------------------
    private sub checkFolderAndCreate(strPath)
    	if blnCheckFolder(strPath) = false then
    		createFolder(strPath)
    	end if
    end sub
    '-----------------------------------------------------------------
    private function blnCheckFolder(strPath)
    	dim objFSO 'as scripting.filesystemobject
    	set objFSO = server.CreateObject("scripting.filesystemobject")
    	if objFSO.FolderExists(strPath) = true then
    		blnCheckFolder = true
    	else
    		blnCheckFolder = false
    	end if	
    	set objFSO = nothing
    end function
    '-----------------------------------------------------------------
    private sub createFolder(strPath)
    	dim objFSO 'as scripting.filesystemobject
    	set objFSO = server.CreateObject("scripting.filesystemobject")
    	objFSO.createFolder(strPath)
    	set objFSO = nothing
    end sub
    '-----------------------------------------------------------------
    end class
    %>
    this script is somehow a bit special for my needs, cause it creates folders if they dont exists, but you can feel free to modify it yourself.

    When you want to upload a file to the server, all you have to do is to first call "initialize" and then call "saveFile(strPath, strFile)", where strPath is the path where you want the file to be saved, and file is the filename.

    If you want to get other value from the form (like textareas, radios etc) you use the "getValFromBin("txtBox","")" function, where the first param. is the name of the item and the second param is left empty (its used for other purposes inside the class).

    To get the filename from the uploaded file you use the property "strGetFileName".

    An example on how the ASP-file can look like:
    Code:
    <%
    'dont forget to include the classFile
    %>
    <!--#include virtual="classFile.asp"-->
    <%
    dim intTypesID 'as integer
    dim strPath 'as string
    dim strFile 'as string
    dim clsUploadFileRef
    
    set clsUploadFileRef = new clsUploadFile
    call clsUploadFileRef.initialize
    
    'gets a value form the hidden named "itemName"
    intTypesID = clsUploadFileRef.getValFromBin("itemName", "") 
    
    'specify the path
    strPath = "/images/"
    
    'get the filename
    strFile = clsUploadFileRef.strGetFileName
    
    'save the file
    call clsUploadFileRef.saveFile(server.MapPath(strPath) &"\", strFile)
    set clsUploadFileRef = nothing
    %>
    ...and the html-file should look something like this:
    Code:
    <form action="nextPage.asp" method="post" ENCTYPE="multipart/form-data">
    <input type="file" name="strFile">
    <input type="hidden" name="itemName" value="1">
    <input type="submit" value="Next">
    </form>
    I hope this solves your problems!
    _________________________________________

    Fredrik Klarqvist
    <a href="mailto:[email protected]@spray.se</a>
    Last edited by fredrik; May 17th, 2001 at 06:29 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width