0

我正在使用 PhoneGap 开发一个移动应用程序,其中一个功能涉及将图像上传到 Web 服务进行处理。我编写了一个托管在 IIS 中的 WCF 服务来接受图像,其合同如下所示:

[ServiceContract]
public interface IImages
{
    [OperationContract(Name="UploadImage")]
    [WebInvoke(UriTemplate = "?file_key={fileKey}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
    ImageResource UploadImage(string fileKey, Stream imageStream);
}

我的 web.config 中的配置部分如下所示:

   <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true">
      <serviceActivations>
        <add service="Services.Images" relativeAddress="images.svc" />
      </serviceActivations>
    </serviceHostingEnvironment>
    <services>
      <service behaviorConfiguration="DefaultServiceBehavior" name="Services.Images">
        <endpoint behaviorConfiguration="DefaultEndpointBehavior" binding="webHttpBinding" bindingConfiguration="PublicStreamBinding" contract="Services.Contracts.IImages" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="PublicStreamBinding"
                maxReceivedMessageSize="2000000000" transferMode="Streamed">
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="DefaultEndpointBehavior">
          <webHttp helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="DefaultServiceBehavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceThrottling maxConcurrentCalls="30" maxConcurrentInstances="30" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

当我尝试使用 PhoneGap 的 FileTransfer 类将文件上传到端点时,从服务返回的响应是 405 Method Not Allowed。我在这里做错了什么?

更新:我的移动应用程序中上传文件的功能如下。此代码以前在指向较旧的 ASMX 服务时运行良好。

    ns.UploadImage = function(){
        //alert(ns.Dictionary['LocalImagePath']);

        var uri = ns.Dictionary['LocalImagePath'];

        try {
            var options = new FileUploadOptions();

            options.fileKey = uri.substr(uri.lastIndexOf('/')+1) + ".jpeg";
            options.fileName = uri.substr(uri.lastIndexOf('/')+1) + ".jpeg";
            options.mimeType = "image/jpeg";

            var ft = new FileTransfer();

            ft.upload(uri, GetServerUrl()+"images.svc?file_key="+options.fileKey, ns.UploadImageSuccess, ns.UploadImageError, options);

        } catch (e) {
            ns.UploadImageError(e);
        }
    };
4

1 回答 1

0

好的,所以我想我想通了。显然,当方法像这样托管在根目录时,如果您没有在 uri 中使用“/”跟随服务的名称,请求将无法正确路由。因此,在上传文件的函数中,我将 ft.upload 行更改为以下内容:

ft.upload(uri, GetServerUrl()+"images.svc/?file_key="+options.fileKey, ns.UploadImageSuccess, ns.UploadImageError, options);

哪个有效。

于 2012-10-01T16:15:59.500 回答