将照片从iphone上传到restful wcf

时间:2012-02-29 07:51:54

标签: iphone wcf rest upload photo

我正在尝试将照片从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];}

来自restful 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无法识别它。我想知道它有什么问题或给我另一个解决方案。任何意见表示赞赏。

1 个答案:

答案 0 :(得分:1)

在这里,您尝试将图像数据发布到Web服务。我不确定这是否是您正在寻找的解决方案。

这是我将图像数据发布到RESTful服务的方式。

您必须将图像数据包装在请求中的边界参数中。下面的代码解释了如何做到这一点。

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