我正在一个需要将文件从iPhone上传到wcf服务的项目中。我没有在wcf和afnetworking上的经验。我已经坚持了好几天,这是我取得的进步:

用于上传文件的WCF服务:请注意,我已从Codeproject website复制了此代码。

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上非常流行,因此我决定使用它。这是我上传文件的代码:

我走了这么远,在这种情况下请帮助我。谢谢你的帮助

好的,这是新信息:

首先,将文件上传到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);
    }
}
}


第二:用于将文件上传到服务器的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”和文件数据“ 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 Link

我真的需要这样做,谢谢您的再次帮助...

最佳答案

尝试以下尺寸:

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];


您不需要传递文件名或post参数中的大小,您可以在接收表单上进行选择。不要忘记服务器端脚本应使用appendPartWithFileData的名称部分来标识文件上传。

以上所有内容都与PHP有关,但对于ASP.NET来说应该相同。

同样像上面的链接不起作用。

干杯

10-07 19:47
查看更多