0

私は PhoneGap を使用してモバイル アプリに取り組んでいます。機能の 1 つは、処理のために画像を 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 に答える