creating a webservice that accepts a file (Stream) doesnt want other params

前端 未结 6 1441
梦毁少年i
梦毁少年i 2021-01-13 22:50

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 serv

相关标签:
6条回答
  • 2021-01-13 23:01

    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.

    0 讨论(0)
  • 2021-01-13 23:02

    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...
    }
    
    0 讨论(0)
  • 2021-01-13 23:05

    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.

    0 讨论(0)
  • 2021-01-13 23:07

    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

    0 讨论(0)
  • 2021-01-13 23:10

    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.

    0 讨论(0)
  • 2021-01-13 23:21

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

    0 讨论(0)
提交回复
热议问题