これは、iPhone から MVC 3 関数へのデータの送信に関する質問になります。
私がすでに行っていることと機能していることのトピックに入るには、接続をどのように実装しているかを確認するだけです。
ここでは、IIS 7.0 で実行される MVC コントローラー関数のサンプルを示します。
SampleController :コントローラー
public ActionResult MyFunction(MyObject object) {
ContentResult result = new ContentResult();
result.Content = some content from the server encoded as JSON
return result;
}
iPhone では、この関数を次のように呼び出します。
NSString *urlString = [NSString stringWithFormat:@"https://%@:%d/%@/%@", _host, _port, Sample, MyFunction];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:json]; // some json encoded object that fits the MyObject from asp.NET
NSURLResponse *response;
NSError *error;
// synchronous so the sample code is shorter
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
// Do some error handling and http status code stuff and finally the json stuff from the NSData object
このようなことはすべて正常に機能していますが、今は混乱するところまで来ています。画像データなどを含む NSData オブジェクトをアップロードする必要があります。this SO answer に示されているように、mulipart/form-data を使用してファイルのみをアップロードします。しかし、いくつかの URL パラメーターを追加すると、機能しなくなります。
________________________________________________________________________________________
したがって、ここから私の問題を解決するための2つのオプションがあります。
最初のアプローチは次のとおりです。このような関数を使用する場合、NSURLRequest はどのように見えるでしょうか? それともこれは可能ですか?
public ActionResult MyFunction(MyObject object, byte[] binaryData) {
// Some server stuff ect.
}
または、最初の関数を使用して添付ファイルを追加して NSURLRequest を作成するにはどうすればよいでしょうか?
編集
現在のファイル アップロード リクエストを作成する方法を次に示します。基本的には、2 番目のコード スニペットと同じです。
NSString *urlString = [NSString stringWithFormat:@"https://%@:%d/%@/%@", _host, _port, Sample, MyFunction];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:json];
最後に添付ファイルです。注パラメータなしで添付ファイルのみを使用する場合、すべて正常に動作しますが、パラメータがありません。
+ (void)attachFile:(NSData *)fileData withFilename:(NSString *)filename toRequest:(NSMutableURLRequest *)request {
NSString *boundary = @"---------------------------94712889831966399282116247425";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; 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=\"userfile\"; filename=\"%@\"\r\n", filename]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:fileData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *oldBody = [request HTTPBody];
if (oldBody) {
[body appendData:oldBody];
}
[request setHTTPBody:body];
}
だから私の推測では、欠落しているパラメーターの境界がいくつかあると思います。
ノート
さらに、バックグラウンド スレッドで同期 NSURLConnection を使用します。
NSURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];