eXept Software AG Logo

Smalltalk/X Webserver

Smalltalk Dynamic Web Pages - Source of WebDemoApp1-test8:

Home

Documentation
www.exept.de
Everywhere
for:
test8:aRequest
    "a dynamic html page to upload a file.
     Invoke via browser as URL:
        host:port/<N>,test8:
     where <N> is the registered name of this service.
    "
    |fileNameChunk multiPartChunks server fileService uploadDirectory uploadDirectoryBase linkFile removeFilesChunk uploadChunk
     uploadedFilenames replyPresenter|

    server := aRequest server.
    uploadDirectoryBase := 'upLoadTest'.
    replyPresenter := aRequest replyPresenter.
    replyPresenter title:'Upload example'.
    replyPresenter parametersAt:#secondHeadLine put:'Test8: Upload example'.
    fileService := server serviceForLink:'/files' ifAbsent:nil.
    fileService notNil ifTrue:[
        uploadDirectory := fileService fileRoot construct:uploadDirectoryBase.
        uploadDirectory exists ifFalse:[
            Error handle:[:err| self error:err description] do:[
                uploadDirectory makeDirectory.
            ].
        ].
        multiPartChunks := aRequest multiPartChunks.
        multiPartChunks notNil ifTrue:[
            aRequest authenticateForRealm:'SysAdmin'.
            "/ arrive here only if authenticated
            removeFilesChunk := multiPartChunks at:#'RemoveFiles' ifAbsent:nil.
            uploadChunk := multiPartChunks at:#'Upload' ifAbsent:nil.
            removeFilesChunk notNil ifTrue:[
                uploadDirectory directoryContentsAsFilenames do:[:aFilename|
                    "/ re-raise to avoid a full backtrace
                    Error handle:[:err| self error:err description] do:[
                        aFilename remove.
                    ].
                ].
            ].
            uploadChunk notNil ifTrue:[
                fileNameChunk := multiPartChunks at:#'fileName' ifAbsent:nil.
                (fileNameChunk notNil and:[fileNameChunk isFilenameChunk]) ifTrue:[
                    fileNameChunk writeFileIntoDirectory:uploadDirectory.
                ].
            ].
        ].
    ].

    aRequest nextPutLine:'Obviously, this requires permissions...<p>'.
    aRequest
        nextPutLine: '<FORM action="', (aRequest linkIdFor:self selector:#'test8:'),'" method="post" enctype="multipart/form-data">';
        nextPutLine:'<input type="file" name="fileName">&nbsp;&nbsp;'.
    aRequest formSubmitButtonForString:'upload' name:'Upload'.
    aRequest nextPutLine:'<br><P><P>'.
    aRequest formSubmitButtonForString:'Remove all uploaded files' name:'RemoveFiles'.
    aRequest formEnd.

    uploadedFilenames := uploadDirectory directoryContentsAsFilenames.
    uploadedFilenames notEmpty ifTrue:[
        aRequest
            nextPutLine: '<P><H2>Uploaded Files:</H2>'.
        uploadedFilenames do:[:aFilename|
            aFilename isDirectory ifFalse:[
                aRequest nextPutLine: '<BR>'.
                linkFile := uploadDirectoryBase, '/', aFilename baseName.
                aRequest nextPutLine: (aRequest webLinkForText:(aFilename baseName) 
                                            service:fileService 
                                            argument:(linkFile)).
            ].
        ].
    ].
    self addLinksForSource:#'test8:' previous:#'test7:' next:#'testGoogle:' to:aRequest.

    "Modified: / 24-07-2011 / 06:55:12 / cg"

Edit (disabled)
Index


ST/X 7.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Sat, 21 Dec 2024 16:08:35 GMT