2011-11-10 13 views
0

私はiPhoneには新しく、自分のアプリケーションでXMLを使用して、サーバーの特定のフォルダに画像を1つ送信する必要があります。オブジェクト参照がオブジェクトのインスタンスに設定されていない

NSString *filename = @"Image"; 
NSData *imageData = UIImageJPEGRepresentation([imageView image], 90); 
NSString *urlString = [NSString stringWithFormat:@"http://111.111.11.1/webservices.asmx/PutImage"]; 

// URLです:http://111.111.11.1/webservices.asmx/PutImage?ImgIn=image.jpg

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setHTTPMethod:@"POST"]; 

NSString *boundary = [NSString stringWithString:@"--------1473780983"]; 
NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded;boundary=%@",boundary]; 
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"images\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:imageData]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[request setHTTPBody:body]; 
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

NSLog(@"\n\n\nreturnString : %@",returnString); 

のように、私はメッセージを取得しています:リターン文字列に「オブジェクト参照がオブジェクトのインスタンスに設定されていません」と同じのために私は、コードを使用しています。誰かが私にいくつかの解決策を案内できますか?あなたのサーバーにあなたのiPhoneからのデータをアップロードする

+0

あなたのreturnDataは空です – sicKo

+0

@sicKo:理由を助けてくれますか? – Sarah

答えて

0

[OK]を...使用してイム私のコードを試してみてください....

// ImageViewのはUIImageView

NSData *imageData = UIImageJPEGRepresentation(imageview.image, 100); 


// setting up the URL to post to 
NSString *urlString = @"http://example.com/upload"; 

// setting up the request object now 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setHTTPMethod:@"POST"]; 

/* 
add some header info now 
we always need a boundary when we post a file 
also we need to set the content type 

You might want to generate a random boundary.. this is just the same 
as my output from wireshark on a valid html post 
*/ 
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

/* 
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-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:imageData]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
// setting the body of the post to the reqeust 
[request setHTTPBody:body]; 

// now lets make the connection to the web 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
self.returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
です
+0

これは私が使用しているのと同じコードです。私はその行に疑問があります[body appendData:[[NSString stringWithString:@ "Content-Disposition:フォームデータ;名前= \"ユーザーファイル\ ";ファイル名= \" .jpg \ "\ r \ n"] dataUsingEncoding:NSUTF8StringEncoding ]];私はname = \ "userfile \"の代わりに渡す必要があります。ファイル名= \ "。jpg – Sarah

+0

名前=あなたのNSData *画像データとファイル名=あなたのNSString *ファイル名 –

+0

大丈夫です....それは全面的な混乱でした。 – Sarah

0

- (void)sendImage { 

       NSData *postData = [nsdata from your original image]; 
       NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

       // Init and set fields of the URLRequest 
       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
       [request setHTTPMethod:@"POST"]; 
       [request setURL:[NSURL URLWithString:[NSString stringWithString:@"http://yoururl.domain"]]]; 

       [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
       [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
       [request setHTTPBody:postData]; 

       NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

       if (connection) { 
        // Return data of the request 
         NSData *receivedData = [[NSMutableData data] retain]; 
       } 
      [request release]; 

     } 
+0

行[要求setURL:[NSURL URLWithString:[NSString stringWithString:@ "http://yoururl.domain"]]]; http://yoururl.domain、Img = image.pngなどのパラメータを渡す場合はどうすればいいですか?私はimage.pngがnsdataによってフェッチされると思いますが、どのようにパラメータをabtですか? – Sarah

+0

[setHTTPBody:postData];あなたの画像データを含んでいます.... –

+0

私は知っていましたが、私はパラメータについて尋ねています。私はURLにその画像を渡す必要があります。 Like imgName = something.png – Sarah

関連する問題