I’m using: SWI-Prolog version 7.6.4
I want the code to: upload an excel file and save it to a specific location.
The following code - uploaded the file and save it somewhere else.
%upload.pl
:- module(upload,
[run/0,upload_form/1]
).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_header)).
:- use_module(library(http/http_multipart_plugin)).
:- use_module(library(http/http_client)).
:- use_module(library(http/html_write)).
:- use_module(library(option)).
:- http_handler(root(.), upload_form, []).
:- http_handler(root(upload), uploads, []).
run :-
http_server(http_dispatch, [port(3211)]).
upload_form(_Request) :-
reply_html_page(
title('Upload a file'),
[ h1('Upload a file'),
form([ method('POST'),
action(location_by_id(uploads)),
enctype('multipart/form-data')
],
table([],
[ tr([td(input([type(file), name(file)]))]),
tr([td(align(right),
input([type(submit), value('Upload!')]))])
]))
]).
uploads(Request) :-
multipart_post_request(Request), !,
http_read_data(Request, Parts,
[ on_filename(save_file)
]),
memberchk(file=file(FileName, Saved), Parts),
format('Content-type: text/plain~n~n'),
format('Saved your file "~w" into "~w"~n', [FileName, Saved]).
uploads(_Request) :-
throw(http_reply(bad_request(bad_file_upload))).
multipart_post_request(Request) :-
memberchk(method(post), Request),
memberchk(content_type(ContentType), Request),
http_parse_header_value(
content_type, ContentType,
media(multipart/'form-data', _)).
:- public save_file/3.
save_file(In, file(FileName, File), Options) :-
option(filename(FileName), Options),
setup_call_cleanup(
tmp_file_stream(octet, File, Out),
copy_stream_data(In, Out),
close(Out)).
prolog:message(bad_file_upload) -->
[ 'A file upload must be submitted as multipart/form-data using', nl,
'name=file and providing a file-name'
].