eXept Software AG Logo

Smalltalk/X Webserver

Smalltalk Dynamic Web Pages - Source of WebDemoApp1-test8:

Home

everywhere
www.exept.de
for:
test8:aRequest
    "a dynamic html page, 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'.
            removeFilesChunk := multiPartChunks at:#'RemoveFiles' ifAbsent:nil.
            uploadChunk := multiPartChunks at:#'Upload' ifAbsent:nil.
            removeFilesChunk notNil ifTrue:[
                uploadDirectory directoryContentsAsFilenames do:[:aFilename|
                    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: '<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
Index


ST/X 7.1.0.0; WebServer 1.653 at exept.de:8081; Sun, 20 Aug 2017 02:03:49 GMT