How to upload a file to web (http) server

2 views
Skip to first unread message

Diem

unread,
Apr 10, 2008, 3:34:35 AM4/10/08
to iPhoneOSCoders
I want to upload a file from iphone to a web server. The web server is
just a regular one that can handle upload form or I can code the sever
in anyway that can be used with iphone.

I was trying to use the ULRRequest, but have no clue how to pass the
file content to the request.

Any help? Thanks.

David Phillip Oster

unread,
Apr 10, 2008, 3:55:21 PM4/10/08
to iPhoneOSCoders
Use an NSURLRequest set the request.HTTPMethod to @"POST" set the
request.HTTPBody to an NSData of the file you want to upload (or use
HTTPBodyStream to an NSINputStream Then, handle this server side like
any webfrom.

David Phillip Oster

unread,
Apr 10, 2008, 3:57:25 PM4/10/08
to iPhoneOSCoders
of course I mean NSMutableURLRequest . Pass it to an NSURLConnection
to do the actual transfer.


Steve Shi

unread,
Apr 12, 2008, 1:56:03 PM4/12/08
to iphoneo...@googlegroups.com
Hi David,

Thanks for your pointer. but... I would like to know:
Is that a conceptual idea that you have in mind which you believe will work this way or have you already implemented this code successfully and it works without a hitch?

Steve

Diem

unread,
Apr 13, 2008, 1:42:16 PM4/13/08
to iPhoneOSCoders
Thank you. After few more googles, I got it done. Here is the snippet
for reference

NSURL *cgiUrl = [NSURL URLWithString:@"http://myserver.com"];
NSMutableURLRequest *postRequest = [NSMutableURLRequest
requestWithURL:cgiUrl];

//adding header information:
[postRequest setHTTPMethod:@"POST"];

NSString *stringBoundary = [NSString
stringWithString:@"0xKhTmLbOuNdArY"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-
data; boundary=%@",stringBoundary];
[postRequest addValue:contentType forHTTPHeaderField: @"Content-
Type"];


//setting up the body:
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r
\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-
Disposition: form-data; name=\"uploadFile\"; filename=\"%@\"\r
\n",filename] dataUsingEncoding:NSUTF8StringEncoding]];
//[postBody appendData:[[NSString stringWithString:@"Content-Type:
application/octet-stream\r\n\r\n"]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:imageData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r
\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];

NSHTTPURLResponse *theResponse = nil;
NSError *theError = nil;
NSString* ret =nil;

NSData *theResponseData = [NSURLConnection
sendSynchronousRequest:postRequest returningResponse:&theResponse
error:&theError];
if (theResponseData == nil || [theResponse statusCode]!=200)
{
return nil;
} else
{
ret = [[NSString alloc] initWithData:theResponseData
encoding:NSUTF8StringEncoding];
}


On Apr 12, 10:56 am, "Steve Shi" <stevesh...@gmail.com> wrote:
> Hi David,
>
> Thanks for your pointer. but... I would like to know:
> Is that a conceptual idea that you have in mind which you believe will work
> this way or have you already implemented this code successfully and it works
> without a hitch?
>
> Steve
>
> On Fri, Apr 11, 2008 at 3:57 AM, David Phillip Oster <
>

Steve Shi

unread,
Apr 23, 2008, 3:00:27 AM4/23/08
to iphoneo...@googlegroups.com
Hi ,
 
This uploading code works fine. Thanks for providing it.
 
Steve

Reply all
Reply to author
Forward
0 new messages