NSData *theimage; |
} |
@property (retain) nsdata *theimage; |
-(void) syncupload:(nsdata *) uploadimage; |
@end |
|
|
#Import "ImageUploader.h" |
|
#define notify_and_leave (X) {[self cleanup:x]; return;} |
#define DATA (x) [x datausingencoding:nsutf8stringencoding] |
|
Posting Constants |
#Define image_content @ "content-disposition:form-data; name=\"%@\ "; Filename=\" Image.jpg\ "\r\ncontent-type:image/jpeg\r\n\r\n" |
#define boundary @ "------------0x0x0x0x0x0x0x0x" |
|
@implementation Imageuploader |
@synthesize theimage; |
|
|
-(void) syncupload:(nsdata *) uploadimage |
{ |
Self. theimage = uploadimage; |
[self main]; |
} |
|
-(void) cleanup: (nsstring *) output |
{ |
Self. theimage = nil; |
NSLog (@ "********%@", output); |
} |
|
-(nsdata*)generateformdatafrompostdictionary:(nsdictionary*)dict |
{ |
ID boundary = boundary; |
nsarray* keys = [dict AllKeys]; |
nsmutabledata* result = [nsmutabledata data]; |
|
for (int i = 0; i < [keys count]; i++) |
{ |
ID value = [dict valueforkey: [Keys objectatindex:i]]; |
[Result appenddata:[[nsstring stringwithformat:@ "--%@\ r \ n", boundary] Datausingencoding:nsutf8stringencoding]]; |
|
if ([Value iskindofclass:[NSData class]]) |
{ |
Handle image data |
NSString *formstring = [nsstring stringwithformat:image_content, [Keys objectatindex:i]]; |
[Result AppendData: DATA (formstring)]; |
[Result Appenddata:value]; |
} |
|
NSString *formstring = @ "\ r \ n"; |
[Result AppendData:DATA (formstring)]; |
} |
|
NSString *formstring =[nsstring stringwithformat:@ "-%@--\ r \ n", boundary]; |
[Result AppendData:DATA (formstring)]; |
return result; |
} |
|
|
Nsoperationqueueによる of non-contemporaneous えてmainにしています. |
-(void) main |
{ |
if (!self. Theimage) { |
Notify_and_leave (@ "Please set image before uploading."); |
} |
nsstring* Multipartcontenttype = [nsstring stringWithFormat:@ "multipart/form-data; boundary=%@ ", boundary]; |
|
nsmutabledictionary* post_dict = [[nsmutabledictionary alloc] init]; |
[Post_dict SetObject:@ "Testimage" forkey:@ "filename"]; |
[Post_dict setobject:self.theimage forkey:@ "data[user][image]"]; |
|
NSData *postdata = [self generateformdatafrompostdictionary:post_dict]; |
[Post_dict release]; |
|
NSString *baseurl = @ "Https://example.com/api/upload_image"; |
Nsurl *url = [nsurl Urlwithstring:baseurl]; |
Nsmutableurlrequest *urlrequest = [nsmutableurlrequest Requestwithurl:url]; |
if (!urlrequest) notify_and_leave (@ "Error creating the URL Request"); |
|
[URLRequest Sethttpmethod: @ "POST"]; |
[URLRequest setvalue:multipartcontenttype Forhttpheaderfield: @ "Content-type"]; |
[URLRequest sethttpbody:postdata]; |
|
Nserror *error; |
Nsurlresponse *response; |
nsdata* result = [nsurlconnection sendsynchronousrequest:urlrequest returningresponse:&response error:&error]; |
NSLog (@ "* * * * result =%@", result); |
|
if (!result) |
{ |
[self cleanup:[nsstring stringwithformat:@ "Upload error: %@", [Error Localizeddescription]]; |
Return |
} |
|
Return Results |
NSString *outstring = [[[nsstring alloc] initwithdata:result encoding:nsutf8stringencoding] Autorelease]; |
NSLog (@ "* * * * * * outstring =%@", outstring); |
[self cleanup:outstring]; |
} |
|