How can I upload a photo to a server with the iPhone?

后端 未结 6 1026
梦谈多话
梦谈多话 2020-11-22 13:56

I\'m writing an iPhone app that takes a photo and then uploads it to a server. How do I upload a photo to a server with Cocoa? I suppose I use NSUrl somewhere.

Tha

6条回答
  •  攒了一身酷
    2020-11-22 14:21

    - (void) uploadImage :(NSString *) strRequest
    {
    if([appdel checkNetwork]==TRUE)
    {
    
    NSString *urlString =[NSString stringWithFormat:@"Enter Url........."];
        NSLog(@"Upload %@",urlString);
    // setting up the request object now
    
    isUploadImage=TRUE;
    totalsize=[[strRequest dataUsingEncoding:NSUTF8StringEncoding]length];
    
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    
    NSString *boundary = [NSString stringWithString:@"_1_19330907_1317415362628"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/mixed; boundary=%@",boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
    [request setValue:@"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2" forHTTPHeaderField:@"Accept"];
    [request setValue:@"2667" forHTTPHeaderField:@"Content-Length"];
    
    
    /*
     now lets create the body of the post
     */
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    
    
    [body appendData:[[NSString stringWithString:@"Content-Type: application/json\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    
    //[body appendData:[NSData dataWithData:imageData]];
    
    [body appendData:[strRequest dataUsingEncoding:NSUTF8StringEncoding]];
    
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];
    
    
    
    theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (theConnection) 
        webData = [[NSMutableData data] retain];
    else 
        NSLog(@"No Connection");    
    }
    
    }
    

提交回复
热议问题