c# - Consume FileStream from WCT Restful service -


i have created wcf restful service, following code:

interface:

[servicecontract] public interface isigservice {     [operationcontract]     [webinvoke(method = "get", responseformat = webmessageformat.xml,         bodystyle = webmessagebodystyle.bare,         uritemplate = "getmultimedia/{id}")]     stream getmultimedia(string id); } 

the code implements service:

public stream getmultimedia(string id) {     string filepath = multimediabll.getfilepath(int.parse(id));     if (string.isnullorempty(filepath))         return null;      try     {         filestream multimediafilestream = file.openread(filepath);         return multimediafilestream;     }     catch (exception ex)     {         console.writeline("not able multimedia stream:{0}", ex.message);         throw ex;     } } 

i need consume service gets filestream multimedia file (image, audio or video) far have this:

restclient client = new restclient(urlservice); restrequest request = new restrequest {     method = method.get,     requestformat = dataformat.xml,     resource = "getmultimedia/{id}" }; request.addparameter("id", idmultimedia, parametertype.urlsegment);  var response = client.execute(request); if (!response.statuscode != httpstatuscode.ok)    return;  ??? 

i don't know how get stream , process it show in handler, this:

context.response.clearcontent(); context.response.clearheaders(); context.response.addheader("content-length", bytes.length.tostring()); context.response.binarywrite(bytes); context.response.end(); 

i guess have file bytes send handler.

i don't know trying do.

service

public stream getmultimedia(string id) {      string filepath = multimediabll.getfilepath(int.parse(id));     if (string.isnullorempty(filepath))         return null;      try     {         filestream multimediafilestream = file.openread(filepath);          weboperationcontext.current.outgoingresponse.contenttype = "your/contenttype";         weboperationcontext.current.outgoingresponse.headers["content-disposition"] = "attachment; filename=your_file_name.png";          return multimediafilestream;     }     catch (exception ex)     {         console.writeline("not able multimedia stream:{0}", ex.message);         throw ex;     } } 

client

restclient client = new restclient(urlservice); restrequest request = new restrequest {     method = method.get,     requestformat = dataformat.xml,     resource = "getmultimedia/{id}" };  request.addparameter("id", idmultimedia, parametertype.urlsegment);  context.response.clear(); content.response.clearheaders();  # write original responsestream context.response.outputstream request.responsewriter = (responsestream) => responsestream.writeto(context.response.outputstream);  var response = client.downloaddata(request);  # maybe need add content type , file name context.response.addheader("content-type", "fetch response"); context.response.addheader("content-disposition", "fetch response");  context.response.flush(); context.response.close(); context.response.end();  if (!response.statuscode != httpstatuscode.ok)     # 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -