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 asp.net to WCF.
I need to done it from iPhone to wcf
my sample of work
WCF:
public class TestService : ITestService
{
[WebInvoke(Method = "POST", UriTemplate = "UploadFile?fileName={fileName}")]
public string UploadFile(string fileName, Stream fileContents)
{
string absFileName = "";
try
{
absFileName = string.Format("{0}\\FileUpload\\{1}"
, AppDomain.CurrentDomain.BaseDirectory
, Guid.NewGuid().ToString().Substring(0,6) + ".jpg");
using (FileStream fs = new FileStream(absFileName, FileMode.Create))
{
fileContents.CopyTo(fs);
fileContents.Close();
}
return "Upload OK";
}
catch(Exception ex)
{
return "FAIL ==> " + ex.Message + " " + absFileName;
}
}
Calling from ASP.NET :
private string UploadFileToService(HttpPostedFileBase file)
{
string url = @" http://192.168.2.3/UploadImage/TestService.svc/REST/UploadFile?fileName=" + file.FileName;
var uri = new Uri(url);
var request = (HttpWebRequest) WebRequest.Create(uri);
request.ContentType = "application/octet-stream";
request.Method = WebRequestMethods.Http.Post;
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);
}
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:
[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