Other APIs
1. wbSaveFile
wbSaveFile is a function designed for file uploads.
This function allows a client to send a file to the server. Similar to wbPutFile(), it is required for sharing data over the web. wbSaveFile() specifies the physical path of the server where the client’s files will be uploaded.
-
Prototype
#include <wbapi.h> #include <atmi.h> int wbSaveFile(WBSVCINFO *rqst, char *key, char *path)
-
Return value
If the function is executed successfully, it returns 1. If an error occurs, it returns -1.
-
Example
<savefile.m>
*DOMAIN webtob1 *NODE tmaxh1 WEBTOBDIR="/user2/haninho/webtob", SHMKEY = 54777, DOCROOT="/user2/haninho/webtob/docs", APPDIR="/user2/haninho/webtob/ap", PORT = “7654” *SVRGROUP htmlg NODENAME = "tmaxh1", SvrType = HTML webapg NODENAME = "tmaxh1", SVRTYPE = WEBSTD *SERVER html SVGNAME = htmlg, MinProc = 1, MaxProc =2 savefile SVGNAME = webapg, MinProc = 1, MaxProc = 2, *SERVICE test SVRNAME = savefile *URI wbapi Uri = "/svct/", Svrtype = WEBSTD *EXT htm MimeType = "text/html", SvrType = HTML
-
Compile the configuration file.
$ wscfl –I savefile.m
-
Create a service table.
$ wsgst
-
Create a source file.
Below is an example of an html file that sends a request.
<savefile.html>
<HTML> <TITLE> wbSaveFile() Test </TITLE> <BODY> <H2> wbSaveFile TEST </H2> <FORM METHOD=post ACTION="/svct/test" enctype='multipart/form-data'> <B>FILE UPLOAD</B> <TABLE WIDTH=650 BGCOLOR=#DDDDDD> <TR> <TD><SMALL><B>file</B></SMALL></TD> <TD><INPUT TYPE=file NAME=upfile SIZE=35></TD> </TR> <TR> <TD><INPUT TYPE=submit VALUE="upload"></TD> </TR></TABLE> </FORM> </BODY> </HTML>
Below is an example WBAPI program that processes the request.
<savefile.c>
#include <stdio.h> #include <usrinc/atmi.h> #include <usrinc/wbapi.h> test(WBSVCINFO *rqst) { char *temp, *filedata, *filename, path[255]; long filelen; int k, count, rtn; FILE *fp; count = wbGetDataCount(rqst); fprintf(stdout,"datacount=%d\n\n",count); for(k=1; k<=count; k++) { temp = wbGetNthKey(rqst, k); fprintf(stdout,"key=%s\n",temp); filedata=wbGetData(rqst, temp); if (filedata != NULL) fprintf(stdout,"data=%s\n",filedata); if((filename=wbGetFileName(rqst, temp)) != NULL) { fprintf(stdout,"key=%s\n",temp); fprintf(stdout,"filename=%s\n",filename); filelen = wbGetFileLen(rqst, temp); fprintf(stdout,"filelength=%ld\n",filelen); sprintf(path, "/user2/haninho/webtob/sample/data/%s", filename); unlink(path); rtn = wbSaveFile(rqst, temp, path); fprintf(stdout,"rtn=%d\n\n",rtn); if( rtn < 0 ) wbPrint(rqst, "File(%s) Upload failed!!\n", filename); else wbPrint(rqst, "File(%s) Upload Success!!<br>\n", filename); } else { filedata=wbGetData(rqst, temp); fprintf(stdout,"key=%s\n",temp); fprintf(stdout,"data=%s\n\n",filedata); wbPrint(rqst, "Key = %s, Data = %s <br>\n\n",temp ,filedata); } } wbReturn(rqst,WBSUCCESS); }
-
Compile the C file created with WBAPI using the Makefile file.
$compile c savefile
-
The following is the html that requests the service with the result displayed on the screen.
When you request the WBAPI service with a browser, you get the following result:
-
2. wbGetErrno
wbGetErrno returns the error number of a WBAPI service.
This function enables WebtoB users to accurately identify error situations by reporting errors configured in the “atmi.h” header file.
-
Prototype
#include <wbapi.h> #include <atmi.h> int wbGetErrno()
-
Return value
If the function is executed successfully, an error number is returned.
-
Example
<geterrno.m>
*DOMAIN webtob1 *NODE tmaxh1 WEBTOBDIR="/user2/haninho/webtob", SHMKEY = 54777, DOCROOT="/user2/haninho/webtob/docs", APPDIR = "/user2/haninho/webtob/ap", PORT = "7654" *SVRGROUP htmlg NODENAME = "tmaxh1", SvrType = HTML webapg NODENAME = "tmaxh1", SvrType = WEBSTD *SERVER html SVGNAME = htmlg, MinProc = 1, MaxProc = 2 geterrno SVGNAME = webapg, MinProc = 1, MaxProc = 2 *SERVICE test SVrname= geterror *URI uri1 uri = "/svct/", SvrType = WEBSTD *EXT htm MimeType = "text/html", SvrType = HTML
-
Compile the environment file (Config-file).
$ wscfl –i geterrno.m
-
Create a service table.
$ wsgst
-
Generate a source file. The following is an example WBAPI program that processes a request, named geterrno.c.
#include <stdio.h> #include <usrinc/atmi.h> #include <usrinc/wbapi.h> test(WBSVCINFO *rqst) { int result; int rnt; int date; wbPrint(rqst, "<HTML>\n"); wbPrint(rqst, "<BODY>\n"); result = wbGetErrno(); wbPrint(rqst, "wbGetErrno() : %d<br>\n",result); date = wbGetDateHdr(rqst, "Modified-Since"); if(date == -1) { result = wbGetErrno(); wbPrint(rqst, "wbGetDateHdr() is failed<br>\n"); wbPrint(rqst, "wbGetErrno() : %d<br>\n", result); } wbPrint(rqst, "</HTML></BODY>"); wbReturn(rqst, WBSUCCESS); }
-
Compile the C file created with WBAPI using the Makefile file.
$ compile pc geterrno
-
The following is the screen where the results are printed.
When you request the WBAPI service with a browser, you get the following result:
-