0

データを WCF サービスにストリーミングするための ajax 呼び出しの例を探しています。私はいつもエラーが発生しています。どんな助けでも感謝します、または解決策のあるブログへのリンクさえも。これは私のWCFサービスクラスです

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Images : IImages
{
    string IImages.UploadImage(string fileKey, Stream imageStream)
    {
        using (var fileStream = File.Create(@"Images\" + fileKey))
        {
            imageStream.CopyTo(fileStream);
        }
        return "done";
    }
}

そして私の契約は

[OperationContract(Name = "UploadImage")]
[WebInvoke(UriTemplate = "?file_key={fileKey}", Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
string UploadImage(string fileKey, Stream imageStream);

私は web.config ストリームバインディングを持っています

<binding name="PublicStreamBinding"
        maxReceivedMessageSize="2000000000" transferMode="Streamed">
    <security mode="None" />
</binding> 

私のajaxクライアント呼び出しはこのようなものです

var data = '{"image":"' + uri + '"}'
$.ajax({
    url: GetServerUrl()+"images.svc/?file_key="+options.fileKey,
    type: "POST",
    contentType: "application/json",
    data: data,
    success: function (result) {
        console.log("SUCCESS");
    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.log("error in transfer::" + jqXHR.responceText);
    }
});
4

2 に答える 2

1

サーバー側のコードについてはコメントできませんが、クライアント側のコードについてはコメントできません。

  • data変数は、JSON 表現ではなく、プレーンな JavaScript オブジェクトである必要があります
  • urlGetServerUrl()プレフィックスは必要ありません。代わりに先頭の「/」を試してください
  • dataPOST リクエストの場合、すべてのパラメータをURL に追加するよりも、オブジェクトに含める方がより一般的です。これが GET アプローチです。サーバー側のコードが何を期待するように設定されているかによって異なりますが、私が知る限り、それはfile_keyPOST に含まれることを期待しています。

あなたはこのようなものになるはずです:

var data = {
    image: uri,
    file_key: options.fileKey
};
$.ajax({
    url: "/images.svc/",//probably
    type: "POST",
    contentType: "application/json",
    data: data,
    success: function (result) {
        console.log("SUCCESS");
    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.log("errror in transfer::" + jqXHR.responceText);
    }
});
于 2012-12-01T16:21:57.543 に答える
0

Install Fiddler ( www.telerik.com/fiddler ). Launch it. Make the web service call. Click on the record of the call in Fiddler. Click on the 'Raw' tabs for request and response. It will be enlightening and you will see exactly what is passed between server and client. Perhaps some addition WCF troubleshooting data as well in the response.

Also, don't forget to check your Application event log on the machine running the WCF service. You can also add a Global.asax to the WCF project (if its a web project) and put logging code in the Application_Error method. Something like this:

    protected void Application_Error(object sender, EventArgs e)
    {       
        Exception ex = Server.GetLastError();

        if (ex is ThreadAbortException)
        {
            // Nothing to do here. The thread abended.
        }
        else
        {
            activityMgr.Add(System.Reflection.MethodBase.GetCurrentMethod(), ex);
        }
    }
于 2015-04-14T21:36:50.690 に答える