I am new to C++. I have written upload file wcf in c# and have gsoap2.8 to generate header.h file.
This is my upload file wcf service in c#.
public void UploadFile(RemoteFileInfo request)
{
FileStream targetStream = null;
Stream sourceStream = request.FileByteStream;
string uploadFolder = @"C:\temp\upload\copyHere";
string filePath = Path.Combine(uploadFolder, request.FileName);
using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
//read from the input stream in 6K chunks
//and save to output stream
const int bufferLen = 65000;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
{
targetStream.Write(buffer, 0, count);
}
targetStream.Close();
sourceStream.Close();
}
}
Below is header.h code generated by gsoap2.8 from a wsdl:
class SOAP_CMAC xsd__base64Binary
{
public:
unsigned char *__ptr;
int __size;
char *id; /* optional element of type xsd:string */
char *type; /* optional element of type xsd:string */
char *options; /* optional element of type xsd:string */
struct soap *soap; /* transient */
public:
virtual int soap_type() const { return 8; } /* = unique id SOAP_TYPE_xsd__base64Binary */
virtual void soap_default(struct soap*);
virtual void soap_serialize(struct soap*) const;
virtual int soap_put(struct soap*, const char*, const char*) const;
virtual int soap_out(struct soap*, const char*, int, const char*) const;
virtual void *soap_get(struct soap*, const char*, const char*);
virtual void *soap_in(struct soap*, const char*, const char*);
xsd__base64Binary() { xsd__base64Binary::soap_default(NULL); }
virtual ~xsd__base64Binary() { }
};
class SOAP_CMAC _ns1__RemoteFileInfo
{
public:
xsd__base64Binary FileByteStream; /* SOAP 1.2 RPC return element (when namespace qualified) */ /* required element of type ns2:StreamBody */
struct soap *soap; /* transient */
public:
virtual int soap_type() const { return 14; } /* = unique id SOAP_TYPE__ns1__RemoteFileInfo */
virtual void soap_default(struct soap*);
virtual void soap_serialize(struct soap*) const;
virtual int soap_put(struct soap*, const char*, const char*) const;
virtual int soap_out(struct soap*, const char*, int, const char*) const;
virtual void *soap_get(struct soap*, const char*, const char*);
virtual void *soap_in(struct soap*, const char*, const char*);
_ns1__RemoteFileInfo() { _ns1__RemoteFileInfo::soap_default(NULL); }
virtual ~_ns1__RemoteFileInfo() { }
};
Here is my test.cpp code. I need to read a file into xsd__base64Binary so I can assign to remoteFile.FileByteStream
BasicHttpBinding_USCOREITransferService svc;
__ns1__UploadFileResponse uploadRespond;
_ns1__RemoteFileInfo remoteFile;
std::string path = "C:\\temp\\upload\\test.txt";
i need to read above file into ????
remoteFile.FileByteStream = ?
so I can call webservice uploadFile as below:
int result = svc.__ns1__UploadFile(&remoteFile, uploadRespond);
Thank you in advance,
JH
Here is an example in c:
unsigned char * buffer;
int fileLen;
FILE *file;
//OPEN FILE
file = fopen(name, "rb");
if (!file)
{
fprintf(stderr, "Unable to open file %s", name);
return;
}
//GET FILE LENGTH
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
//ALLOCATE MEMORY
buffer=(unsigned char *)malloc(fileLen+1);
if (!buffer)
{
fprintf(stderr, "Memory error! :( ");
fclose(file);
return;
}
//READ FILE CONTENTS INTO BUFFER AND CLOSE FILE
fread(buffer, fileLen, 1, file);
fclose(file);
//USE THE BUFFER ....
//FREE YOUR ALLOCATED BUFFER
free(buffer);
In your remoteFile:
remoteFile.FileByteStream.__ptr = buffer;
remoteFile.FileByteStream.__size = fileLen;
UPDATE: allocate memory with soap_malloc
Use:
buffer=(unsigned char *)soap_malloc(soap,fileLen+1);
Instead of:
buffer=(unsigned char *)malloc(fileLen+1);
Remove:
free(buffer);
Allocation will be done in the context of the current operation, the free will be done by the gsoal library when the operation is done.