我试图通过 phonegap filetransfer.upload 将文件发布到 wcf 休息服务。当我将文件发送到服务时,我设法上传了所有文件,但后来我的日志显示不支持 HttpContext。我尝试允许 asp 兼容性要求,但无济于事,我发现与 webOperationContext 没有等效项。我会很感激任何帮助,我卡住了。这是相关代码:
接口:
[OperationContract]
[WebInvoke(UriTemplate = "putFile",
ResponseFormat = WebMessageFormat.Json,
Method = "POST") ]
bool putFile();
}
执行:
public bool putFile()
{
try
{
var request = OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
String driverId = request.Headers["driverId"];
HttpPostedFile file = HttpContext.Current.Request.Files["file"];
String fileName = file.FileName;
int _driverId = Int32.Parse(driverId);
String fileURI = makeLocalPath(file.ContentType, fileName,driverId);
if (file != null)
{
Entities db = new Entities();
var uri = db.dt_media_uri.Where(p => p.driver_id == _driverId && p.uri_value.Equals(fileName));
using (Stream inputStream = file.InputStream)
{
FileStream fileStream = new FileStream(fileURI, FileMode.Create, FileAccess.Write, FileShare.Read);
if (fileStream == null)
{
try
{
inputStream.CopyTo(fileStream);
}
catch { return false; }
try
{
foreach (var v in uri)
{
v.validated = true;
}
}
catch { EventLog.WriteEntry("Application", "unable to validate uri for driverID: " + driverId); }
try
{
db.SaveChanges();
}
catch { return false; }
}
}
}
return true;
}
catch(Exception e) { EventLog.WriteEntry("Application", "unable process filename\n"+e.ToString()); return false; }
网络配置:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<connectionStrings>
<add name="Entities" connectionString=*non relevant*"/>
</connectionStrings>
<system.web>
<customErrors mode="Off"/>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true">
</serviceHostingEnvironment>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="NewStandardEndpoint0" />
</webHttpEndpoint>
</standardEndpoints>
<services>
<service name="PullOverWcf.Service1">
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="crossDomain" name="REST" contract="PullOverWcf.IService1" />
<host>
<baseAddresses>
<add baseAddress="http://localhost" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="crossDomain" crossDomainScriptAccessEnabled="true" maxReceivedMessageSize="21000000"/>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept, driverID" />
</customHeaders>
</httpProtocol>
<security>
<!--authentication>
<windowsAuthentication enabled="true" />
</authentication-->
</security>
</system.webServer>
</configuration>
这是来自客户:
options.chunkedMode = false;
options.headers = {
driverId : self.driverId
};
console.log(JSON.stringify(options));
var ft = new FileTransfer();
ft.upload(path, encodeURI(self.serverPath + "/putFile"), fileSuccess,
fileFail, options);