2012-02-29 6 views
1

iphoneから安らかなwcfに写真をアップロードしようとしています。私は何度も試しましたが、失敗しました。ここでは、クライアント写真をiphoneから安らかなwcfにアップロード

-(IBAction)uploadClick:(id)sender{ 
NSString *surl = @"http://192.168.5.226:8068/FileService.svc/UploadFile" ; 
NSURL *url = [NSURL URLWithString:surl]; 

ASIFormDataRequest *r = [ASIFormDataRequest requestWithURL:url]; 
[r setValidatesSecureCertificate:NO]; 
[r setTimeOutSeconds:30]; 
[r setRequestMethod:@"POST"]; //default is POST (insert), 
[r setDelegate:self]; 
[r setDidFailSelector:@selector(requestDidFail:)]; 
//[r addRequestHeader:@"Content-Type" value:@"application/json"] this will cause the call to fail. No content-type header for this call. 

NSMutableData *imageData = [NSMutableData dataWithData:UIImageJPEGRepresentation([UIImage imageWithData:self.pickedFileContent], .35)]; 
[r setPostBody:imageData]; 
[r setDidFinishSelector:@selector(imageSaveDidFinish:)]; 
[r startAsynchronous];} 

から鉱山のいくつかのコード

コード安らかなWCFからのコードです:

public interface IFileService 
{ 
    [OperationContract, WebInvoke(UriTemplate = "UploadFile", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")] 
    void UploadFile(Stream fileContent); 
} 
public class FileService : IFileService 
{ 
    public void UploadFile(Stream fileContent) 
    { 
      convert the stream to a file and save it. 
    } 
} 

私はiphoneからuploadClickイベントをトリガする場合、画像データはNSMutableDataに変換されますが、 [r startAsynchronous]の後に、何も起こりませんでしたwcf。おそらくリクエストがwcfに届かなかったか、wcfがそれを認識できなかったのかもしれない。私はそれに何が間違っているかを知りたい、あるいは私に別の解決策を与えたい。コメントは感謝しています。

+0

wcfについてはわかりません。しかし、画像やその他のデータをアップロードするには、マルチパートリクエストモデルを使用します。 http://stackoverflow.com/questions/9477046/how-to-send-image-data-to-server-i-already-have-a-sample-page/9477819#9477819、http://stackoverflow.com/質問/ 9479093/how-to-upload-image-data-to-server-specific-with-file-name#comment11996956_9479093 – Ravin

答えて

1

ここでは、画像データをWebサービスにPOSTしようとしています。これがあなたが探している解決策であるかどうかはわかりません。

これは、画像データをRESTfulサービスにPOSTする方法です。

リクエストの境界パラメータにイメージデータをラップする必要があります。以下のコードは、それを行う方法を説明しています。

NSURL *url = [NSURL urlWithString:@"http://www.sample.com/websevice.php"]; 
NSMutableRequest *uRequest = [[NSMutableRequest alloc]initWithUrl:url]; 
//STEP1 Creating NSData from UIImage 
NSData *postImageData = UIImageJPEGRepresentation(image, .7); 
NSMutableData *body = [NSMutableData data]; 

//STEP2 Creating Boundary 
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 

//Setting POST Header 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
[uRequest addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

//STEP3 Begin boundary 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

//STEP4 Adding additional parameters 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n",fileName] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 

//STEP5 Appending Image Data 
[body appendData:[NSData dataWithData:postImageData]]; 


//STEP6 Closing boundary 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

// setting the body of the post to the reqeust 
[uRequest setHTTPBody:body]; 
// IMPLEMENT NSURLConnectionDelegate Protocol 
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:uRequest delegate:self]; 
+0

あなたは安らかなwcfに投稿していますか?ここにあなたのwcfコードを掲載できますか? – user418751

+0

はい、NSURLConnectionを使用しています。私はあなたに完全なコードを与えることはできません。私は詳細を含める答えを編集中です – Krrish

+0

これを参照してくださいhttp://stackoverflow.com/questions/6011595/upload-image-from-iphone-to-wcf-service – Krrish

関連する問題