Naviserver File Upload Example

Upload Example NaviServer

LostOne Hi, welcome to to the fascinating world of NaviServer where you've got the whole world at your fingertips, documentation everywhere, OpenAcs and other cool software with a great development team

 but almost no beginner tutorials or any howto's.. I'm afraid you'll need to spent a lot of time searching the net and looking at examples of other software developed using aolserver/naviserver to "get it".

I'm pretty sure what you need is this: http://naviserver.sourceforge.net/n/naviserver/files/ns_getformfile.html good luck!

Uploading is done in multiple ways in NaviServer, there's even a cool way to spool the upload too a different thread in the background that won't eat your RAM by using temporary files. But for now we'll imagine you're trying to upload 1 file.

First thing's first create a file.. in this case index.adp

<%
#If your server is only set for .adp replace this by
#ns_adp_include main.adp
ns_adp_include -tcl -nocache main.tcl

set return [uploadfile]
ns_puts $return
ns_adp_include  -cache 10 form.adp
%>

To upload a file you need to set the form to enctype="multipart/form-data" otherwise you will never upload the file correctly, this will fustrate you and you'll lose HOURS. Had the same problem in websh for apache..

form.adp

         <form  enctype="multipart/form-data" method="post"> 
        Title  <input name="titlu" type="text"><br>
        Author <input name="autor" type="text"><br>
        File <input name="fisier" type="file"><br>
         <input name="submit" value="Trimte!" type="submit">
        </form>

main.tcl/main.adp Create a folder upload in your pagepath (mine is www yours could be anything in the settings)

#Verify upload info
proc uploadfile {} {
        set output ""
        if {[ns_queryexists submit]} {
                foreach key {titlu autor fisier} { set $key [ns_queryget $key] }
                if {$titlu == ""} {
                        append output "<br>Title must not be empty!"
                        
                }
                if {$autor == ""} {
                        append output "<br>Author must not be empty!"
                                
                }
                if {$fisier == ""} {
                        append output "<br>File must not be empty!"
                        
                }
        
                #append output "DEBUG $titlu $autor $fisier<br>
                #contenftfile [ns_conn contentfile] files info [ns_conn files]
                #<br>                "
                
                #copy the file from the temporary location to the correct location
                set savefile [ns_pagepath]/$fisier
                file copy -force  -- [ns_getformfile fisier] $savefile
#this should copy multiple files, however it doesn't  yet work correctly, another workaround has to be implemented 
        #        foreach file [ns_conn files] {
                        #file copy -force  -- [ns_getformfile $file] [ns_pagepath]/upload/$fisier
                        #ns_puts "An copiat fisierul $file la locatia $fisier"                
        #        }

                if {$output == ""} {
                        ns_puts "$savefile $titlu $autor"
                }
                
        }
        
        

Question/Request

Hello, Im trying to learn Naviserver, but got stuck with file upload, and cant figured it out. Can you please provide some practical example how to access and save uploaded file to folder in server. For example:

<form  action="save_files" method="post" enctype="application/form-data">
        <input type="file" id="fileinput" name="f1" />
        <input type="file" id="fileinput" name="f2"/>
        <input type="file" id="fileinput" name="f3"/>
        <button name="send" type="submit">SEND</button>
</form>

So in this case how proc to save files should look like? Why ns_conn files not returning values?

proc save_files {} {
        set r [ns_conn form]
        set f1 [ns_set get $r f1]
        set f2 [ns_set get $r f2]
        set f3 [ns_set get $r f3]
        ??????????
}

Thank you. Sorry for dumb questions.