将图像上传到WCF服务抛出iPhone xCode [英] Upload Image to WCF service throw iPhone xCode
问题描述
大家好,
希望它回答问题的正确位置,它在.Net和xCode编程之间混合。
我发布了使用iphone上传图片投放网络服务的问题。
我已经完成了从asp.net到WCF的纯粹调用。
我需要从iPhone完成它到wcf
我的工作样本
WCF:
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)
{
//save file
string absFileName = "";
try
{
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;
//System.IO.File.Create(absFileName);
using (FileStream fs = new FileStream(absFileName, FileMode.Create))
{
fileContents.CopyTo(fs);
fileContents.Close();
}
return "Upload OK";
}
catch(Exception ex)
{
return "FAIL ==> " + ex.Message + " " + absFileName;
}
}
从ASP.NET调用:
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 = @" http://192.168.2.3/UploadImage/TestService.svc/REST/UploadFile?fileName=" + file.FileName;
// string url = @"http://amrmk185-001-site1.smarterasp.net/UploadMe/TestService.svc/REST/UploadFile?fileName=" + 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();
}
}
我已经完成搜索从xCode做同样的事情
该文件已上传,但文件不正确,尺寸为differanet
以下我做过的样本
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
推荐答案
您可以配置对IIS Express的远程访问
我搜索它:
http://开发人员.xamarin.com / guides / cross-platform / application_fundamentals / web_services / walkthrough_working_with_WCF / [ ^ ]
you can Configuring Remote Access to IIS Express
I search it:
http://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/walkthrough_working_with_WCF/[^]
这篇关于将图像上传到WCF服务抛出iPhone xCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!