admin管理员组

文章数量:1277603

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.

[Error]

Operation 'NewImage' in contract 'IFormServices' has multiple request body parameters, one of which is a Stream. When the Stream is a parameter, there can be no other parameters in the body.

[interface]

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

[definition]

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]

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

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

How would this be done?

edit: put error code at top.

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.

[Error]

Operation 'NewImage' in contract 'IFormServices' has multiple request body parameters, one of which is a Stream. When the Stream is a parameter, there can be no other parameters in the body.

[interface]

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

[definition]

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]

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

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

How would this be done?

edit: put error code at top.

Share Improve this question asked Nov 1, 2013 at 16:06 FallenreaperFallenreaper 10.7k15 gold badges75 silver badges139 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 3

This post: How to: Create a Service That Accepts Arbitrary Data using the WCF REST Programming Model describes another method of posting a stream along with some data.

They show how to send the file name (but you can add and/or replace that with any string parameter) along with the file.

The contract is:

[ServiceContract]
public interface IReceiveData
{
    [WebInvoke(UriTemplate = "UploadFile/{strParam1}/{strParam2}")]
    void UploadFile(string strParam1, string strParam2, Stream fileContents);
}

The exposed service will accept the stream via POST along with the parameters that were defined.

It's an issue\bug with WCF, which don't accept any other parameters when using Stream input.

We also had similar issue with WCF and after all research we decided to convert other input parameters also into stream and attach it to the input with some delimter

Just a thought - how about using HTTP headers? You can then process using WebOperationContext.IningRequest.

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...
}

How about using HttpRequest.QueryString[]?

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

you call it through the URL like:

\NewImage?server={server}&datasource={datasource}&document={doc}&image_id={id}

Then in your code:

public string NewImage(Stream imgStream)
{
    var request = System.Web.HttpContext.Current.Request;

    var server= request.QueryString["server"];
    var datasource = request.QueryString["datasource"];
    var document= request.QueryString["document"];
    var image_id= request.QueryString["image_id"];

    ...
}

I'd been looking for something like this for a while, and just stumbled across it today.

If the string result parameter on your NewImage method is some kind of unique identifier, you could create a second method called something like NewImageAttributes which accepts the extra data, along with the unique identifier, and then you could tie the data together again in your service.

Of course, this would mean two calls to the service, but it may solve your issue.

本文标签: ccreating a webservice that accepts a file (Stream) doesnt want other paramsStack Overflow