sailorproject / sailor

A Lua MVC Web Framework.
MIT License
921 stars 125 forks source link

How to receive image data from client #130

Closed sunyiyou closed 7 years ago

sunyiyou commented 7 years ago

Well, I'm totally fresh in this framework. I need to create a web service to receive an image from client and save to disk. I have read the documents and found it curt. I still have no idea about how to parse a "POST" request and read image data with multipart-form.

Could you please recommend some materials to read or give me some instructions about it?

Etiene commented 7 years ago

Hello @sunyiyou, sorry for the delay :) Yes, it's true, the file upload is not well documented in Sailor. The reason is because how this works can vary between different webservers and we still didn't build a layer on the top of this to make it uniform.

Which webserver are you using with Sailor?

For xavante, the one that comes already bundled, page.POST will bring a table with and you will find the file information in there and a file pointer to a tmp file.

XAVANTE EXAMPLE: View views/mycontroller/upload.lp:

<form enctype="multipart/form-data" method="post">
    <input type="file" name="my_image">
    <input type="submit" value="Send">
</form>

Controller controllers/mycontroller.lua:

local mycontroller = {}
function mycontroller.upload(page)
    if page.POST.my_image then
        local image = page.POST.my_image
        local file = io.open ('/a/path/'..image.filename, 'w')
        io.output(file)
        io.write(image.file:read('*all'))
        io.close(file)
    end
    page:render('upload')
end
return mycontroller

Please tell me if this works for you or if you need directions for another webserver :)

sunyiyou commented 7 years ago

Firstly, thanks for your response. Well, our my application is like a cloud vision web service. In the most situations, the upload of image is not based on the web browser. I finally solve this by transmitting byte code(maybe not so elegant).

later, I encountered another trouble. My system is Mac OS 10.11. I found that any packages to be required in controllers' files is none. I also tried (https://github.com/sailorproject/sailor_website), controllers which require some important packages can not work either. I don't know if I have made some stupid mistakes or the bug does exists. If you have some spare time, it worth a try.

Etiene commented 7 years ago

Hello! So you mean it's going to be done through a post request? When you say solved by transmitting byte code, what was exactly your solution, could you elaborate more?

For other issues, could you please open a different thread? :)

sunyiyou commented 7 years ago

Yes, the post request does work. By transmitting byte code, it does work, too, but not so elegant. We simply read the url params like "localhost:8080/?data=&&**……". Thanks for your help.