Click here to Skip to main content
15,903,817 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys,

Hope it correct place to answer question,it mix between .Net and xCode programming.

I have issue of upload image throw web service using iphone.

I have done it with pure calling from to WCF.
I need to done it from iPhone to wcf

my sample of work

public class TestService : ITestService
       [WebInvoke(Method = "POST", UriTemplate = "UploadFile?fileName={fileName}")]
       public string UploadFile(string fileName, Stream fileContents)
           //save file
           string absFileName = "";
                 absFileName = string.Format("{0}\\FileUpload\\{1}"
                                       , AppDomain.CurrentDomain.BaseDirectory
                                       , Guid.NewGuid().ToString().Substring(0,6)  + ".jpg");

              // string fld = @"h:\root\home\amrmk185-001\www\publish\WCFService\FileUpload\" + fileName;
                 using (FileStream fs = new FileStream(absFileName, FileMode.Create))
               return "Upload OK";
           catch(Exception ex)
               return "FAIL ==> " + ex.Message + "   " + absFileName;

Calling from ASP.NET :

private string UploadFileToService(HttpPostedFileBase file)
    //specify the url with fileName parameter
   // string url = @"http://localhost:8080/TestService.svc/REST/UploadFile?fileName=" + file.FileName;
    string url = @"" + file.FileName;

   //  string url = @"" + file.FileName;
    var uri = new Uri(url);

    //create HttpWebRequest
    var request = (HttpWebRequest) WebRequest.Create(uri);
    request.ContentType = "application/octet-stream";
    request.Method = WebRequestMethods.Http.Post;

    //set request stream (Content)
    using (var requestStream = request.GetRequestStream())
        byte[] fileDataInByte = null;
        using (BinaryReader binaryReader = new BinaryReader(file.InputStream))
            fileDataInByte = binaryReader.ReadBytes(file.ContentLength);
        requestStream.Write(fileDataInByte, 0, fileDataInByte.Length);

    //Call REST and get a response back
    using (var response = request.GetResponse())
        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
        return reader.ReadToEnd();

I have done search to do same from xCode
The file is uploaded ,but not correct file,size is differanet

Following sample of I Have done

UIImage * img = [UIImage imageNamed:@"Stonehenge.jpg"];

    NSData *imageData = UIImageJPEGRepresentation(img,0.2);

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://xxxxt/UploadMe/TestService.svc/REST/UploadFile?fileName=ffff.jpg"]];

    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"xxxxBoundaryStringxxxx";

    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];

    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

    NSMutableData *body = [NSMutableData data];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"iphoneimage.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[NSData dataWithData:imageData]];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];

    [request addValue:[NSString stringWithFormat:@"%d", [imageData length]] forHTTPHeaderField:@"Content-Length"];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"%@", returnString);

Hope that help
thanks all
Rohant Kunnat 7-May-15 4:39am    
Hello Amr,

Did you manage to solve your problem? I have encountered the same problem as yours. The file copied is corrupted.

Rohant K.
BalaThakur 24-Jun-15 5:26am    
Same problem here too. If possible, Can you please provide me solution?
BalaThakur 24-Jun-15 5:25am    
Same problem here too. If possible, Can you please provide me solution?

1 solution

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900