iOS デバイスからWCFサーバへファイルをアップロードする方法

WCFサーバ側 C#プログラム
WCF でのエンドポイント、バインディング、コントラクトなどは適当に設定しておく。

公開インタフェース定義

1
2
3
4
5
6
7
  [ServiceContract(Namespace = "http://My.WCF.Samples")]
  public interface IService1
  {
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "UploadFile/{fileName}")]
    string UploadFile(string fileName, Stream fileContents);
  }

アップロードされたファイルデータを保存する
クライアントから受信したファイルデータを c:¥ 直下に保存する

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace HelloWCF2
{
  public class Service1 : IService1
  {
    public string UploadFile(string fileName, Stream fileContents)
    {
        Console.WriteLine("received file name : {0}", fileName);
        byte[] buffer = new byte[1000000];
        int bytesRead, totalBytesRead = 0;
        do
        {
            bytesRead = fileContents.Read(buffer, 0, buffer.Length);
            totalBytesRead += bytesRead;
        } while (bytesRead > 0);
        Console.WriteLine("Uploaded file {0} with {1} bytes", fileName, totalBytesRead);
 
        System.IO.FileStream fs = new System.IO.FileStream(@"C:\myphoto.png",
                                                            System.IO.FileMode.Create,
                                                            System.IO.FileAccess.Write);
        fs.Write(buffer, 0, totalBytesRead);
        fs.Close();
 
        return "画像を受信しました";
    }
  }
}

iOS デバイス側 objective-c プログラム
iOS デバイスから .png 画像をアップロードするパターン1 (ASIFormDataRequest を使用する場合)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    // PNG画像を WCF サーバへ送信する
    // 1. ASIFormDataRequest を使用する場合
    NSURL *url = [NSURL URLWithString:@"http://192.168.x.x:8000/WCFSampleService/HelloWCF/UploadFile/myphoto.png"];
    request_ = [ASIFormDataRequest requestWithURL:url];
 
    [request_ setPostValue:@"myphoto1.png" forKey:@"fileName"];
 
    UIImage *image = [UIImage imageNamed:@"myphoto1.png"];
    NSData* imageData = [[NSData alloc] initWithData:UIImagePNGRepresentation(image)];
    [request_ appendPostData:imageData];
 
    [request_ setRequestMethod:@"POST"];
 
    [request_ setDidFinishSelector:@selector(requestFinished:)];
    [request_ setDidFailSelector:@selector(requestFailed:)];
    [request_ setDelegate:self];
    [request_ startAsynchronous];

iOS デバイスから .png 画像をアップロードするパターン2 (NSURLConnection を使用する場合)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    // 2. NSURLConnection を使用する場合
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"http://192.168.0.8:8000/WCFSampleService/HelloWCF/UploadFile/myphoto.png"]];
    [request setHTTPMethod:@"POST"];
 
    NSMutableData *postBody = [NSMutableData data];
    UIImage *image = [UIImage imageNamed:@"myphoto1.png"];
    NSData* imageData = [[NSData alloc] initWithData:UIImagePNGRepresentation(image)];
    [postBody appendData:[NSData dataWithData:imageData]];
    [request setHTTPBody: postBody];
 
    // 非同期通信
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (conn == nil) {
        // エラー処理
    }