开发者

Getting problem when uploading the data in server in iphone sdk

I am getting a problem when I am uploading the data to server. Actually my code seems like this:

-(void)publishToServer:(AddDetailsObject *)aCustObj
{

 NSString *urlString = @"http://219.91.165.17:8080/uploadIphone/upload.jsp";
 UIImage *theImage = [UIImage imageWithData:custObj.picture];
 NSData *imageData = UIImageJPEGRepresentation(theImage,0.9);

 //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
 //NSString *uniquePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@".caf"];
 printf("\n recordedTmpFile......%s",[recordedTmpFile UTF8String]);
 NSData *audioData = [[NSData alloc] initWithData:[recordedTmpFile dataUsingEncoding:NSASCIIStringEncoding]];
 printf("\n length of data...%d",[data length]);

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

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

 NSMutableData *postBody = [NSMutableData data];
 if([custObj.userName length]>0)
 {
  [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:custObj.userName] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 }
 if([custObj.phoneNumber length]>0)
 {
  [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"phoneNo\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:custObj.phoneNumber] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

 }
 if([custObj.audioDuration length]>0)
 {
  [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"duration\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:custObj.audioDuration] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 }
 if([custObj.imeiNumber length]>0)
 {
  [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"imei\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:custObj.imeiNumber] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 }
 if([custObj.latitude length]>0)
{
  [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"latitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:custObj.latitude] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 }
 if([custObj.longitude length]>0)
 {
  [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"longitude\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:custObj.longitude] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 }
 if([custObj.messageTitle length]>0)
 {
  [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:custObj.messageTitle] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 }
if([custObj.picture length]>0) //Here Im getting the problem in server returndata I am getting only image but the audio file is not getting if the audio file code is above this picture code then audio file is returned but picture is not returned.
 {
  [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"picture\"; filename=\"test.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:imageData];
  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 }
 if([audioData length]>0)
 {
  [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"audio.caf\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody append开发者_如何学CData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postBody appendData:audioData];
  [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
 }
  [request setHTTPBody:postBody];
 NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
 NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
 printf("\n return string:%s",[returnString UTF8String]);

}

Is there any limit for uploading data to server?

Please guys get me out of this problem.

Hope I will get a quick response from your side. Thanks in advance, Monish.


I guess you haven't encoded the file content properly. According to the RFC2388 you need to encode the content of the files before writing it to the request. As the whole request uses 7-bit character encoding so unless you send ascii text files you need to encode the content of the files somehow.

You can search the RFC section 6 for the correct encoding and fix your code, but I would recomend using an open source library that does that for you.

Here is how you send forms using ASIHttpRequest:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];

Check this one: http://allseeing-i.com/ASIHTTPRequest/ I was using it succesfully for http. They claims support for multipart/form-data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜