Skip to content Skip to sidebar Skip to footer

Creating A Webservice That Accepts A File (stream) Doesnt Want Other Params

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 requ

Solution 1:

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]
publicinterfaceIReceiveData
{
    [WebInvoke(UriTemplate = "UploadFile/{strParam1}/{strParam2}")]
    voidUploadFile(string strParam1, string strParam2, Stream fileContents);
}

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

Solution 2:

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

Solution 3:

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

Solution 4:

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

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

Solution 5:

How about using HttpRequest.QueryString[]?

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

you call it through the URL like:

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

Then in your code:

publicstringNewImage(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.

Post a Comment for "Creating A Webservice That Accepts A File (stream) Doesnt Want Other Params"