C# and VC6 Web Services File Upload
I'm cross posting, sorry, but this pertains to both sides..
I have a Web Services written in C# and a client written in VC6. I've
never uploaded files through web services so forgive my ignorance in this
matter, but I'm trying to understand how I can do this from the C++ client.
The C# code works for HTTP Form Posts, but not really what I'm looking for..
This should be for applications to upload files too.. The code is as
follows:
-----------------------------------------------------------------
[WebMethod]
public bool UploadFileCollection()
{
try
{
//HTTP Context to get access to the submitted data
HttpContext postedContext = HttpContext.Current;
//File Collection that was submitted with posted data
HttpFileCollection Files = postedContext.Request.Files;
//Make sure a file was posted
//This works for HTTP Form Posts, need to understand how to make this
work for C++ posts
string fileName = (string)postedContext.Request.Form["fileName"];
if (Files.Count == 1 && Files[0].ContentLength > 1 && fileName != null
&& fileName != "")
{
//The byte array we'll use to write the file with
byte[] binaryWriteArray = new
byte[Files[0].InputStream.Length];
//Read in the file from the InputStream
Files[0].InputStream.Read(binaryWriteArray, 0,
(int)Files[0].InputStream.Length);
//Open the file stream
FileStream objfilestream = new FileStream("c:\\" + fileName,
FileMode.Create, FileAccess.ReadWrite);
//Write the file and close it
objfilestream.Write(binaryWriteArray, 0,
binaryWriteArray.Length);
objfilestream.Close();
return true;
}
else
{
return false;
}
}
catch (Exception ex1)
{
throw new Exception("Problem uploading file: " + ex1.Message);
}
}
-----------------------------------------------------------------
I have VC6 client that is to upload a file to the C# Web Services Interface
using WinHttp..
WinHttpOpen
WinHttpConnect
WinHttpOpenRequest
WinHttpSetOption
WinHttpAddRequestHeaders
WinHttpWriteData //header
WinHttpWriteData //binary file in data chunks
WinHttpWriteData //footer
WinHttpReceiveResponse
WinHttpQueryHeaders
WinHttpQueryDataAvailable
WinHttpReadData
WinHttpCloseHandle
I've never written anything in C++ to talk to Web Services before and I know
this code work for file uploads to IIS via ASPUpload object on the server.
I may be way off, but my searching is leading me no where.