2

私はiphoneからwcfサービスにファイルをアップロードする必要があるプロジェクトに取り組んでいます。私はwcfとafnetworkingの両方の経験がありません。私は何日もこのステップに固執してきました、そしてここに私が成し遂げた進歩があります:

ファイルをアップロードするためのWCFサービス:このコードをCodeprojectWebサイトからコピーしたことに注意してください。

public interface ITransferService
{
[OperationContract]
RemoteFileInfo DownloadFile(DownloadRequest request);

[OperationContract]
 void UploadFile(RemoteFileInfo request); 
}

    public void UploadFile(RemoteFileInfo request)
{
    FileStream targetStream = null;
    Stream sourceStream =  request.FileByteStream;

    string uploadFolder = @"C:\upload\";

    string filePath = Path.Combine(uploadFolder, request.FileName);

    using (targetStream = new FileStream(filePath, FileMode.Create, 
                          FileAccess.Write, FileShare.None))
    {
        //read from the input stream in 65000 byte chunks

        const int bufferLen = 65000;
        byte[] buffer = new byte[bufferLen];
        int count = 0;
        while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
        {
            // save to output stream
            targetStream.Write(buffer, 0, count);
        }
        targetStream.Close();
        sourceStream.Close();
    }

}

アップロードコードは、sourcodeに付属しているクライアントプログラムで適切に機能します。wcfサービスを介して、任意のサイズ、任意のタイプまたはファイルをアップロードできます。

また、AFNetworkingフレームワークはiOSで非常に人気があることがわかったので、それを使用することにしました。ファイルをアップロードするための私のコードは次のとおりです。

私はここまで来ました、この状況で私を助けてください。助けてくれてありがとう

OK、ここに新しい情報があります:

まず第一に、ファイルをwcfサービス(動作している)にアップロードするためのc#コード

protected void Button1_Click(object sender, EventArgs e)
{ 
if (FileUpload1.HasFile)
{
    System.IO.FileInfo fileInfo = 
           new System.IO.FileInfo(FileUpload1.PostedFile.FileName);
    FileTransferServiceReference.ITransferService clientUpload = 
           new FileTransferServiceReference.TransferServiceClient();
    FileTransferServiceReference.RemoteFileInfo 
           uploadRequestInfo = new RemoteFileInfo();

    using (System.IO.FileStream stream = 
           new System.IO.FileStream(FileUpload1.PostedFile.FileName, 
           System.IO.FileMode.Open, System.IO.FileAccess.Read))
    {
        uploadRequestInfo.FileName = FileUpload1.FileName;
        uploadRequestInfo.Length = fileInfo.Length;
        uploadRequestInfo.FileByteStream = stream;
        clientUpload.UploadFile(uploadRequestInfo);
        //clientUpload.UploadFile(stream);
    }
}
}

2番目:ファイルをサーバーにアップロードするために使用されたremotefileinfoクラス:

  public class RemoteFileInfo : IDisposable
  {
    [MessageHeader(MustUnderstand = true)]
     public string **FileName**;

    [MessageHeader(MustUnderstand = true)]
    public long **Length**;

    [MessageBodyMember(Order = 1)]
    public System.IO.Stream **FileByteStream**;

    public void Dispose()
    { 
        if (FileByteStream != null)
       {
        FileByteStream.Close();
        FileByteStream = null;
       }
    }   
  }

これらすべてのコードから、「Filename」「FileLength」とfiledata「FileByteStream」を含むリクエストを作成する必要があることを理解しています。コードで何かを試しましたが、このコードで画像をアップロードしようとすると、サーバーでエラー415が発生します。

   AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://192.168.2.121:85"]];

UIImage *image = [UIImage imageNamed:@"test.jpg"];
NSData *data = UIImageJPEGRepresentation(image, 0.2);

NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:@"test.jpg" forKey:@"FileName"];
[parameters setObject:[NSString stringWithFormat:@"%i",data.length] forKey:@"Length"];

NSMutableURLRequest *myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"/webservice/Transferservice.svc/UploadFile" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:data name:@"RemoteFileInfo" fileName:@"test.jpg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:myRequest];
[operation 
 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSLog(@"success: %@", operation.responseString);
 } 
 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"error: %@", operation.error);
 }
 ];

[[[NSOperationQueue alloc] init] addOperation:operation];

サービスのWSDLリンクもここにあります:

WSDLリンク

私は本当にこれをする必要があります、もう一度助けてくれてありがとう...

4

3 に答える 3

3

サイズについてこれを試してください:

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://192.168.2.121:85"]];

NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"test.jpg"], 0.2);

NSMutableURLRequest *myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"/webservice/Transferservice.svc/UploadFile" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:data name:@"RemoteFileInfo" fileName:@"test.jpg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:myRequest] autorelease];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *op, id responseObj) {
    NSLog(@"success: %@", operation.responseString);
} failure:^(AFHTTPRequestOperation *op, NSError *error) {
    NSLog(@"[Error]: (%@ %@) %@", [operation.request HTTPMethod], [[operation.request URL] relativePath], operation.error);
}];

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:operation];

ファイルの名前やサイズを投稿パラメーターに渡す必要はありません。受信フォームでこれを取得できます。appendPartWithFileDataファイルのアップロードを識別するために、サーバー側スクリプトで の名前部分を使用する必要があることを忘れないでください。

上記はすべて PHP に関連していますが、ASP.NET でも同じように機能するはずです。

また、上記のようにリンクが機能しません。

乾杯

于 2011-11-25T22:30:20.280 に答える
1

最後に、ファイルストリーム経由でファイルを WCF にアップロードすることに成功しました。問題は、以前のコードがヘッダーとストリームを本文に期待していたため、それを行う方法が見つからなかったことです。代わりに、ストリームをパラメーターとしてのみ受け入れるコードを作成する必要があることがわかりました。その後、ファイル名やその他の処理はサーバー側で行われます。

私はこの質問から wcf サービスのアップロード コードを取得しました:ポスト エンコードされた multipart/form-data を受け入れる WCF サービス

答えに書かれていることを正確に行いました。iPhone側では、操作用の入力ストリームをセットアップしました。ファイルを wcf サービスにアップロードするための AFNetworking のコードを次に示します。

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://192.168.2.121:85"]];

UIImage *image = [UIImage imageNamed:@"test.jpg"];
NSData *data = UIImageJPEGRepresentation(image, 0.2);
NSInputStream *stream = [[[NSInputStream alloc]initWithData:data] retain];
NSDictionary *parameters = nil;

NSMutableURLRequest *myRequest = [client requestWithMethod:@"POST" path:@"/uploadservice/service1.svc/Upload" parameters:nil];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:myRequest];

 [
 operation 
 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
     NSLog(@"success: %@", operation.responseString);
 } 
 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"error: %@", operation.error);
 }
 ];

operation.inputStream = stream; //Thats where you put your stream!

[[[NSOperationQueue alloc] init] addOperation:operation];

皆さんの辛抱に感謝します。あなたの返信が完全な回答ではなかったとしても、あなたの返信が私を正しい答えに導いてくれました...

于 2011-11-27T14:09:32.313 に答える
0

リモート Web サーバーから 404 または「見つかりません」というエラーが返されます。

これはおそらく、URL パスまたはサーバー名が間違っていることを意味します。サーバー側の設定に関する情報が不足しているため、正しい URL を推測することができません。

于 2011-11-23T14:36:57.233 に答える