views:

802

answers:

4

I have found numerous examples on uploading a file to a web server via C++ on Windows.

However I am struggling after a lot of searching (and I thought I was good with Google!) to find any examples to help me achieve the same on a Mac.

Can anyone point me towards some help on how to upload a file to a web server on a Mac OS using either C++ or objective C?

It can be via either HTTP/HTTPS or FTP/FTPS (or both).

Thanks!

+1  A: 

You could use 'libcurl', see this article on Wikipedia.

Eduard - Gabriel Munteanu
+1  A: 

connection Kit might be what your looking for

Ben Reeves
+1  A: 

You would want to use an NSURLConnection with a 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 modify or set headers using the NSMutableURLRequest method:

- (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field

The response will be whatever the server comes back with. You can check out Apple's documentation for the rest of the delegate methods to implement to get the body of the response back. You should have the NSData object representing the content of the file you want to upload ready. Doing the same using FTP is a bit more involved, but this will work to post the file body up. You will want to make sure your NSData object is set up like the body of an HTTP post, such that you set up 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 should append the body. On the server side, you can get the file name and the bytes composing the file.

This is not exactly the code you should use, but it should give you a good idea of how to proceed.

Heat Miser
A: 

Hi iam using ConnectionKit Framework in my application.. I am facing the problem in multiple connection to the ftp.. and also in multiple file uploading .. please anyone can guide me in this..

Thanks

The answers section is not the place for questions - try posting the specifics of your problem as a question on here using the "Ask Question" link at the top right. Be as detailed as possible about your set up and your problem.
joshcomley