我正在尝试将 iphone 拍摄的照片上传到WCF RESTful 服务器,我已经在网上查看了有关该问题的示例和其他答案,但我无法给出解决方案。
该应用程序是用iOS6编写的,我正在使用AFNetworking 2.0上传图像。由于在模拟器中相机不起作用,我一直在尝试使用图书馆照片。我认为问题在于格式差异,WCF 正在请求 Stream 参数,我不知道 AFNetworking 是如何发送信息的......
这是WCF 代码:
[WebInvoke(Method = "POST", UriTemplate = "FileUpload")]
public void FileUpload(Stream stream)
{
try
{
byte[] buffer = new byte[10000];
stream.Read(buffer, 0, 10000);
FileStream f = new FileStream("C:\\temp\\sample.jpg", FileMode.OpenOrCreate);
f.Write(buffer, 0, buffer.Length);
f.Close();
stream.Close();
System.Console.WriteLine("Recieved the image on server");
}
catch (Exception e)
{
System.Console.WriteLine("ERROR: " + e.ToString());
}
}
iOS 代码:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"Users/juan/Library/Application Support/iPhone Simulator/6.1/Media/DCIM/100APPLE/IMG_0001.JPG"];
[manager POST:@"http://192.168.2.3:8732/wave/FileUpload" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
在fiddler中测试 Web 服务,它通过发送图像415 unsupported media引发相同的错误。
我让服务器的app.config:
<configuration>
<system.web>
<httpRuntime maxRequestLength="2097151"
useFullyQualifiedRedirectUrl="true"
executionTimeout="14400" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfJsonRestService.waveWS">
<endpoint address="http://localhost:8732/wave"
binding="wsHttpBinding"
bindingConfiguration="wsHttp"
contract="WcfJsonRestService.IWaveWS"/>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="wsHttp" maxReceivedMessageSize ="50000000" messageEncoding="Mtom" maxBufferPoolSize="50000000" >
<readerQuotas maxArrayLength="50000000" />
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
<system.diagnostics>
任何想法或建议如何解决这个问题?欢迎每一个想法
编辑: 最后我自己解决了,问题出在 app.config 中。
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="WebConfiguration"
maxBufferSize="65536"
maxReceivedMessageSize="2000000000"
transferMode="Streamed">
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WcfJsonRestService.waveWS">
<serviceMetadata httpGetEnabled="true" httpGetUrl="" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WcfJsonRestService.waveWS">
<endpoint address="http://localhost:8732/wave"
binding="webHttpBinding"
behaviorConfiguration="WebBehavior"
bindingConfiguration="WebConfiguration"
contract="WcfJsonRestService.IWaveWS"/>
</service>
</services>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>