DanielV
DanielV

Reputation: 2690

Consume FileStream from WCT Restful service

I have created a WCF Restful service, with the 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 that implements this 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 to get multimedia stream:{0}", ex.Message);
        throw ex;
    }
}

I need to consume this service that gets the filestream for a multimedia file (image, audio or video) so far I 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 to get the stream and process it to show it in a handler, something like this:

context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.AddHeader("Content-Length", bytes.Length.ToString());
context.Response.BinaryWrite(bytes);
context.Response.End();

I guess I have to get the file bytes to send it to the handler.

Upvotes: 1

Views: 930

Answers (1)

Kari-Antti Kuosa
Kari-Antti Kuosa

Reputation: 376

I don't know is this something what you are trying to 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 to get 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 to the context.Response.OutputStream
request.ResponseWriter = (responseStream) => responseStream.WriteTo(context.Response.OutputStream);

var response = client.DownloadData(request);

# maybe you need to add content type and file name as well
context.Response.AddHeader("Content-Type", "fetch from response");
context.Response.AddHeader("Content-Disposition", "fetch from response");

context.Response.Flush();
context.Response.Close();
context.Response.End();

if (!response.StatusCode != HttpStatusCode.OK)
    # do something

Upvotes: 2

Related Questions