BBjFileUpload

Description

In BBj 14.00 and higher, BBjFileUpload contains all of the information related to files uploaded to a Servlet.

Creation

BBxServletContext > BBxServletRequest > BBjFileUpload

A BBjFileUpload object is obtained with the following BBxServletRequest method:

Return Value Method
BBjFileUpload getFileUpload(string fieldName)

Deprecated Creation

BBjServletEvent > BBjHttpRequest > BBjFileUpload

Return Value Method
BBjFileUpload BBjHttpRequest::getFileUpload(string name)
BBjFileUpload BBjspWebRequest::getFileUpload(string name)

Methods of BBjFileUpload

Return Value

Method

long

getContentLength()

string

getContentType()

string

getFieldName()

string

getOriginalName()

string

getTempName()

Remarks

The example below demonstrates how to upload files to a servlet. When the servlet is executed, the uploaded files are stored temporarily and it is the responsibility of the developer to copy the files during the servlet execution cycle. All the files are removed when the servlet execution ends.

The form used for the HTTP Post needs to specify: enctype='multipart/form-data'

The form elements should have unique name identifiers which can be used with the getFileUpload(string name) method.

Example

REM Obtain the instance of the BBjAPI object
LET myAPI!=BBjAPI()
data! = BBjAPI().getServletData(err=publish)

MyServlet! = new MyServlet()
 
data!.setCallback(data!.ON_WEB_CONNECTION, myServlet!, "myMethod")
 
PROCESS_EVENTS
 
class public MyServlet
  method public void myMethod(BBjServletEvent p_event!)
    LET chan = UNT
    request! = p_event!.getHttpRequest()
    response! = p_event!.getHttpResponse()
    response!.setContentType("text/html")
    open(chan)"JSERVLET"
    print(chan)"<html><body><h1>Hello BBj!</h1>"
    declare auto BBjVector files!
    files! = request!.getFileUploads()
    sz = files!.size()
    if sz then
      declare BBjFileUpload uploadFile!
      print(chan)"There were " + str(sz) + " files uploaded.<br>"
      for i=0 to sz-1
        uploadFile! = cast(BBjFileUpload,files!.get(i))
        print(chan)"Field:" + uploadFile!.getFieldName()
        print(chan)"- Name:"+ uploadFile!.getOriginalName()
        print(chan)"- Temp Name:"+ uploadFile!.getTempName()
        print(chan)"- Type:"+ uploadFile!.getContentType()
        print(chan)"- Size:"
        print(chan)uploadFile!.getContentLength()
        print(chan)"<br>"
      next i
    else
      print(chan)"There were no files uploaded, please upload some:<br>"
      print(chan)"<form id='upload' method='post' action='upload' enctype='multipart/form-data' >"
      print(chan)"<input id='file1' name='file1' type='file' /><br>"
      print(chan)"<input id='file2' name='file2' type='file' /><br>"
      print(chan)"<input id='file3' name='file3' type='file' /><br>"
      print(chan)"<input type='submit' value='submit' id='submit' />"
      print(chan)"</form>"
 
    fi
    print(chan)"</body></html>"
    close(chan)
  methodend
classend

publish:

admin! = BBjAPI().getAdmin("admin", "admin123")
registry! = admin!.getServletRegistry()
config! = BBjAPI().getConfig()
appServer! = admin!.getWebAppServer()

application! = appServer!.makeEmptyAppConfig()
application!.setProgramName(pgm(-1))
application!.setWorkingDirectory(dsk("")+dir(""))
application!.setConfigFile( BBjAPI().getConfig().getCurrentCommandLineObject().getConfigFile())
application!.setClasspathName(BBjAPI().getConfig().getCurrentCommandLineObject().getOriginalClasspathName())

bridgeurl$="/upload"
registry!.unpublish(bridgeurl$, err=*next)
registry!.publish(bridgeurl$, application!)

release

See Also

BBjAPI

BBxServletContext

BBxServletResponse

BBjCookie

BBxWebSession

See the BBj Object Diagram for an illustration of the relationship between BBj Objects.