2012-02-29 52 views
1

我正嘗試將iphone上的照片上傳到restful wcf。我嘗試了很多次,但都失敗了。下面是從客戶端礦山將iPhone上的照片上傳到restful 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];} 

從寧靜的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]之後,沒有任何事情發生它restful wcf。我想也許請求沒有達到wcf或wcf無法識別它。我想知道它有什麼問題或給我另一種解決方案。任何意見表示讚賞。

+0

我不確定wcf。但是,對於上傳圖片或其他數據,我們使用多部分請求模型。 http://stackoverflow.com/questions/9477046/how-to-send-image-data-to-server-i-already-have-a-sample-page/9477819#9477819,http://stackoverflow.com/問題/ 9479093 /如何上傳圖像數據到服務器與特定文件名#comment11996956_9479093 – Ravin 2012-02-29 08:42:20

回答

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

您發佈到一個安寧的wcf?你可以在這裏發佈你的wcf代碼嗎? – user418751 2012-03-01 05:49:12

+0

是的,我正在使用NSURLConnection。我無法給你完整的代碼。我正在編輯答案以包含更多詳細信息 – Krrish 2012-03-05 06:56:44

+0

請參閱http://stackoverflow.com/questions/6011595/upload-image-from-iphone-to-wcf-service – Krrish 2012-03-05 07:14:24