5

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.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129

6 Answers6

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.

OSH
  • 2,847
  • 3
  • 25
  • 46
1

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

Prem Sriram
  • 190
  • 5
  • Is there a way (IE9 stable) which will convert a set of parameters into a stream, and append the 2 streams together? I would LOVE to see a demo of this. – Fallenreaper Nov 01 '13 at 16:22
1

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

EdSF
  • 11,753
  • 6
  • 42
  • 83
  • That could be interesting. If i decide to do that, how how i formulate the Form request to have those headers? – Fallenreaper Nov 01 '13 at 18:20
  • @Fallenreaper **if I'm not mistaken**, the form's use is just to add your vars to the request. See if doing the [POST itself via Ajax + custom headers](http://stackoverflow.com/questions/7100294/json-post-with-customized-httpheader-field/7100389#7100389) can replace it. Server-side is [similar](http://msdn.microsoft.com/en-us/library/system.net.webclient.headers.aspx) – EdSF Nov 01 '13 at 21:21
1

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...
}
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
1

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.

Zac Faragher
  • 963
  • 13
  • 26
0

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.

SimonGoldstone
  • 5,096
  • 3
  • 27
  • 38
  • 1
    NewImage does return a unique key, but the paramters are needed to create the key, and once a file is created, it cant be modified at all. Standby though, and i will ping the server team quick – Fallenreaper Nov 01 '13 at 16:28
  • OK, waiting for your response, but if possible, upload the image to a temporary place (table or filesystem, etc.) and generate a Guid. Then upload the params with the Guid from step one. When the service receives the params, it retrieves the Image from the temporary area, does the original work, generates the proper key and returns it as normal. Wouldn't that work? – SimonGoldstone Nov 01 '13 at 16:33
  • 1
    Server team says "Thats a no-go Maverick. Go back to the drawing board." -__- I think they watched TopGun recently – Fallenreaper Nov 01 '13 at 18:20
  • Well since it's Friday, you get a +1 for the TopGun reference, although they should have said "That's a negative ghostrider, the pattern is full." – SimonGoldstone Nov 01 '13 at 19:10