File upload via HTTP / HTTPS and / or FTP / FTPS on Mac
I found many examples of uploading a file to a web server via C ++ on Windows.
However, I dread after a lot of searching (and I thought I was good with Google!) To find examples that would help me achieve the same thing on Mac.
Can anyone point me to some help on how to upload a file to a web server on Mac OS using either C ++ or Object C?
It can be HTTP / HTTPS or FTP / FTPS (or both).
Thanks!
a source to share
You would like to use NSURLConnection with NSMutableURLRequest, something like this:
NSMutableURLRequest *theRequest=[[NSMutableURLRequest alloc] init];
[theRequest addValue:@"attachment;filename=\"file2.gif\"" forHTTPHeaderField:@"Content-disposition"];
[theRequest addValue:@"image/gif" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"binary" forHTTPHeaderField:@"Content-Transfer-Encoding"];
[theRequest setHttpBody:myBodyNSDataObject];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData that will hold
// the received data
// receivedData is declared as a method instance elsewhere
receivedData=[[NSMutableData data] retain];
} else {
// inform the user that the download could not be made
}
You can change or set headers using the NSMutableURLRequest method:
- (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field
The answer will be whatever the server returns with. You can check the Apple documentation for the rest of the delegate methods for implementation to return the response body. You must have an NSData object that represents the content of the file you want to load. Doing the same using FTP is a little more active, but this will work to post the body of the file up. You will want to make sure that your NSData object is configured as the body of the HTTP message, so that you configure the headers like:
[theRequest addValue:@"attachment;filename=\"file2.gif\"" forHTTPHeaderField:@"Content-disposition"];
[theRequest addValue:@"image/gif" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"binary" forHTTPHeaderField:@"Content-Transfer-Encoding"];
And then you have to add a body. On the server side, you can get the filename and bytes that make up the file.
This is not the code you should be using, but it should give you a good idea of how to proceed.
a source to share