创建一个接受文件(流)的网络服务不需要其他参数

Fal*_*per 5 javascript c# asp.net web-services

我有一个文件想要上传到 Web 服务,但它需要额外的参数,因此我创建了一些带有关联名称:值对的隐藏字段以推送到服务器请求。但问题在于服务的定义。

[错误]

合约“IFormServices”中的操作“NewImage”具有多个请求正文参数,其中之一是 Stream。当Stream是参数时,body中不能有其他参数。

[界面]

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

[定义]

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";
        }
Run Code Online (Sandbox Code Playgroud)

[JavaScript代码]

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();
            } );
Run Code Online (Sandbox Code Playgroud)

所以我想找出一种方法将 4 个字符串+一个文件发送到服务器。

这将如何完成?

编辑:将错误代码放在顶部。

Fal*_*per 1

当您发送流时,它实际上会发送请求中的所有内容。我这样做是为了获取数据:

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...
}
Run Code Online (Sandbox Code Playgroud)