创建接受文件web服务(流)不想要其他PARAMS [英] creating a webservice that accepts a file (Stream) doesnt want other params

查看:74
本文介绍了创建接受文件web服务(流)不想要其他PARAMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,我想上传到Web服务,但它需要额外的参数,可以让我创建一​​些隐藏的字段与关联的名称:值对推送到服务器的请求。这个问题虽然是服务的定义。

I have a File i want to upload to a Webservice, but it needs additional params, so I create some hidden fields with the associated name:value pairs to get pushed to the server request. The issue though is the definition of the service.

[错误]

工作合同IFormServices'NewImage'有多个请求体的参数,其中一个是流。当流是一个参数,可以有身体没有其他的参数。

[接口]

[OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json)]
    string NewImage(Stream data, string server,string datasource, string document, string image_id);

[定义]

public string NewImage(Stream data, string server, string datasource, string document, string image_id)
        {
        //this should, similar to others, need a server, datasource, and some sort of document in which to append the images.
        WebClient wsb = new WebClient();
        string str = "_URL_";
        byte[] byte_data = new byte[data.Length];
        data.Read(byte_data, 0, byte_data.Length);
        byte[] response = wsb.UploadData(str,"POST",byte_data);
        string retVal = Convert.ToString(response);
        //want to return a JSON.serialized dictionary of:  given image_id + id returned from response.
        Dictionary<string, object> retDict = new Dictionary<string, object>();
        retDict["filename"] = image_id;
        retDict["id"] = "";
        //return new JavaScriptSerializer().Serialize(json);
        return "-1";
        }

[JavaScript的code]

[javascript code]

var $form = $("<form />").attr({
                method: "POST",
                enctype: "multipart/form-data",
                target: "image_processing",
                action: "webservices/FormServices.svc/NewImage",
                id: "push_image_to_server"
            } ).appendTo( "body" );
            var im_id = $( this ).attr( "image_id" );
            $( this ).appendTo( "form#push_image_to_server" );
            $( "<input type='hidden' />" ).attr( { name: "server", value: BASE_URL } ).appendTo( $form );
            $( "<input type='hidden' />" ).attr( { name: "datasource", value: SELECTED_DATASOURCE } ).appendTo( $form );
            $( "<input type='hidden' />" ).attr( { name: "document", value: SELECTED_DOCUMENT } ).appendTo( $form );
            $( "<input type='hidden' />" ).attr( { name: "image_id", value: im_id } ).appendTo( $form );
            $("iframe#image_processing").bind("load", function (a,b,c) {
                console.log("SUCCESS", arguments);
                $( "iframe#image_processing" ).unbind( "load", function (a,b,c)
                {
                    console.log( arguments );
                    _IMAGE_UPLOADS_[a["filename"]] = a["id"];
                } );
                $( "form#push_image_to_server" ).remove();
            } );

所以我试图找出一种方法来4串+文件发送到服务器。

So i am trying to figure out a way to send up 4 strings + a file to the server.

这将如何做?

编辑:把错误code。在顶部

edit: put error code at top.

推荐答案

当你发送一个流,它会实际发送请求的一切。我这样做是为了获得数据:

When you send a stream, it will actually send everything in the request. I did this to get the data:

public string NewImage(Stream data){
    NameValueCollection PostParameters = HttpUtility.ParseQueryString(new StreamReader(data).ReadToEnd());
    string server = PostParameters["server"],
    string datasource = PostParameters["datasource"], 
    string document = PostParameters["document"];
    string image_id = PostParameters["image_id"];
    var img = PostParameters["File"];

    //do other processing...
}

这篇关于创建接受文件web服务(流)不想要其他PARAMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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