工作例如AJAX文件上传到WCF服务 [英] Working example of AJAX file upload to WCF service

查看:109
本文介绍了工作例如AJAX文件上传到WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个Ajax调用的数据流,以WCF服务的一个例子。我总是得到一个错误。 任何帮助AP preciated,甚至链接博客一个解决方案。 这是我的WCF服务类

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)
公共类图片:IImages
{
    字符串IImages.UploadImage(字符串的FileKey,流的ImageStream)
    {
        使用(VAR FILESTREAM = File.Create(@影像\+的FileKey))
        {
            imageStream.CopyTo(FILESTREAM);
        }
        返回完成;
    }
}
 

和我的合同是

  [OperationContract的(名称=UploadImage)
[WebInvoke(UriTemplate =?file_key = {}的FileKey,方法=POST,ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Bare)
字符串UploadImage(字符串的FileKey,流的ImageStream);
 

我的web.config流结合

 <绑定名称=PublicStreamBinding
        maxReceivedMessageSize =20亿transferMode =流媒体>
    <安全模式=无/>
< /装订>
 

我的AJAX客户端调用是这样的

  VAR数据='{形象:'+ URI +}
$阿贾克斯({
    网址:GetServerUrl()+?images.svc / file_key =+ options.fileKey,
    键入:POST,
    的contentType:应用/ JSON
    数据:数据,
    成功:函数(结果){
        的console.log(成功);
    },
    错误:函数(jqXHR,textStatus,errorThrown){
        的console.log(+ jqXHR.responceText的转会::错误);
    }
});
 

解决方案

我无法在服务器端code注释,但客户端:

  • 数据变量应该是一个普通的JavaScript对象,而不是一个JSON再presentation
  • 网​​址应该不需要 GetServerUrl() preFIX;尝试了领先的/,而不是
  • 对于一个POST请求是比较正常的,包括在数据的所有参数对象,而不是跟踪他们到的URL,这是GET方法。这取决于什么样的服务器端code设置期望,但据我所知,预计 file_key 要在后。

您应该结束了这样的事情:

  VAR数据= {
    图片:URI,
    file_key:options.fileKey
};
$阿贾克斯({
    网址:/images.svc/",//probably
    键入:POST,
    的contentType:应用/ JSON
    数据:数据,
    成功:函数(结果){
        的console.log(成功);
    },
    错误:函数(jqXHR,textStatus,errorThrown){
        的console.log(errror的转会::+ jqXHR.responceText);
    }
});
 

I'm looking for an example of an ajax call for streaming data to a WCF service. I am always getting an error. Any help appreciated, or even links to blogs with a solution. This is my WCF service class

[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";
    }
}

and my contract is

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

I have web.config stream binding

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

my ajax client call is like this

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);
    }
});

解决方案

I can't comment on the server-side code, but client-side :

  • the data variable should be a plain javascript object, not a JSON representation
  • url shouldn't need the GetServerUrl() prefix; try a leading "/" instead
  • for a POST request it's more normal to include all parameters in the data object rather than tacking them onto the URL, which is the GET approach. It depends what the server-side code is set up to expect but as far as I can tell, it expects file_key to be in the POST.

You should end up with something like this :

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);
    }
});

这篇关于工作例如AJAX文件上传到WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆