javascripthtmlluaopenwrtluci

Unable to upload file from GUI


I'm trying to upload file in a custom view. The file is uploaded in the following form:

<form id="logo-splash-form" action="<%=luci.dispatcher.build_url("admin/system/upload")%>">
    <input id="logo-splash" name="logo-splash" type="file" />
    <input type="button" value="Upload" onclick="fileUpload('logo-splash')" />
</form>

with the following js function:

function fileUpload(fileName)
{
    var url = '<%=luci.dispatcher.build_url("admin/system/upload")%>';
    document.getElementById('logo-splash-form').enctype = 'multipart/form-data';
    document.getElementById('logo-splash-form').submit();
}

and the following controller:

function upload()
    local fp
    local sys = require "luci.sys"  
    local path = "/etc/mypath/"
    local ul = luci.http.formvalue("logo-splash")
    local file   = "test.jpg"
    -- FILE UPLOAD  
    luci.http.setfilehandler(
        function(meta, chunk, eof)
            if not fp then
                fp = io.open(path .. meta.file, "w")
            end
            if chunk then
                fp:write(chunk)
            end
            if eof then
                fp:close()
            end
        end
    )
    luci.http.redirect(luci.dispatcher.build_url('admin/system/splashscreen'))
end

However nothing happens. The file is not created, and I see no message error on the console with logread.

I don't know why setfilehandler seems to be not called, and I'm now stuck with this issue...

I'm testing the upload with a small jpg file, test.jpg, around 10ko, so I don't think it's a size issue.

How to make setfilehandler successfully upload my file? Thanks in advance.


Solution

  • In the end I replaced the button by a submit button without calling any function:

    <input type="submit" class="cbi-input-apply" value="<%:Upload%>" />
    

    The upload process I used was similar to the one you can see in controller/admin/system.lua, in the function action_flashops()