2011-11-10 43 views
0

我是iPhone新手,在我的應用程序中使用XML,我需要將一個圖像發送到服務器的一個特定文件夾。出於同樣的我使用的代碼:未將對象引用設置爲對象的實例在XML中

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

//網址是: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); 

我得到的消息,如:在返回字符串「未設置爲一個對象的實例對象引用」 。任何人都可以指導我解決一些問題嗎?

+0

your returnData is empty – sicKo

+0

@sicKo:你能幫我解釋一下原因嗎? – Sarah

回答

0

好吧......我嘗試使用的代碼,即時通訊....

// 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:form-data; name = \」userfile \「; filename = \」。jpg \「\ r \ n」] dataUsingEncoding:NSUTF8StringEncoding ]];我需要通過什麼來代替name = \「userfile \」; filename = \「。jpg – Sarah

+0

name =你的NSData * imageData和文件名=你的NSString *文件名 –

+0

好吧....那就是整個混亂。謝謝你的解決方案。 – Sarah

0

從iPhone上傳數據到服務器:

- (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

在該行中[request setURL:[NSURL URLWithString:[NSString stringWithString:@「http://yoururl.domain」]]];如果我想傳遞像http://yoururl.domain那麼Img = image.png。我想這image.png將被nsdata提取,但如何參數? – Sarah

+0

[request setHTTPBody:postData];包含您的圖像數據.... –

+0

我knw,但我詢問的參數。我需要傳遞的URL中的圖像。像imgName = something.png – Sarah

相關問題