i am working on a project that need to upload files from iphone to wcf service. i dont have experience on both wcf and afnetworking. i've stuck on this step for days and heres the progress that i've made:
WCF Service for uploading files: note that i've copied this code from 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();
}
}
The upload code works good on the client program came with the sourcode, i can upload any size and any type or files through the wcf service.
I've also found that AFNetworking framework is quite popular on ios, so i've decided to use it. heres my code for uploading file:
i've come this far, please help me in this situation. thanks for helping
OK, heres the new information:
Fırst of all, the c# code to upload file to the wcf service (which is working)
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);
}
}
}
Second: The remotefileinfo class that used to upload file to server:
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;
}
}
}
From all those code i understand that i need to create a request that contains "Filename" "FileLength" and the filedata "FileByteStream". i tried something in the codes but the server gives error 415 when i try to upload image with this code:
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];
also here is the WSDL Link for the service :
i really need to do this, thanks for helping again...