Webサービスを使用して、選択した画像を画像ビューアからサーバーにアップロードします。画像ファイルをサーバーにアップロード中にエラーが発生しました
私のWebサービスでは、ユーザーID、pwd、およびファイル名が要求されます。
しかし、私が実行すると、ファイル名に誤りがあります。
エラーは、「手順には、指定されていないファイル名が必要です」と表示されます。私はここでファイル名を渡していますが。
誰でもコードを見て、私に教えてください。何が問題なのでしょうか? - ありがとう。あなたの他の分野で
NSData *imageData;
imageData = UIImageJPEGRepresentation(imageView.image, 90);
//creating the url request:
NSURL *postUrl = [NSURL URLWithString:@"myURL"];
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postUrl];
//adding header information:
[postRequest setHTTPMethod:@"POST"];
NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
//setting up the body:
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"100"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"pwd\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"pwd"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"1.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: image/jpg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[NSData dataWithData:imageData]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:nil error:nil];
NSString *returnString;
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
lblLoginStatus.text = @"Uploading done.";
NSLog(@"%@",returnString);
私はちょうどサーバーに画像をアップロードしようと数日間を費やしましたが、本当に多くの変数があります。 iOS側ではなくサーバー側で問題になる可能性があります。 私はいくつかのことをお勧めしますか? 1.次の行をコメントアウトします。 [postBody appendData:[[NSString stringWithString:@ "Content-Type:image/jpg \ r \ n \ r \ n"] dataUsingEncoding:NSUTF8StringEncoding]; 2.コンテンツの長さを次のように追加します。 [addValue:[NSString stringWithFormat:@ "%d"、[body length]] forHTTPHeaderField:@ "Content-Length"]; – John