从WCT RESTful服务消费的FileStream [英] Consume FileStream from WCT Restful service

查看:115
本文介绍了从WCT RESTful服务消费的FileStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个WCF RESTful服务,用下面的代码:

I have created a WCF Restful service, with the following code:

接口:

[ServiceContract]
public interface ISIGService
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "GetMultimedia/{id}")]
    Stream GetMultimedia(string id);
}

这是实现此服务的代码:

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;
    }
}



我需要消耗此服务,获取FILESTREAM为多媒体文件(图片,音频或视频)到目前为止,我有这样的:

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.

推荐答案

我不知道这是不是你正在尝试做的。

I don't know is this something what you are trying to do.

服务

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;
    }
}



客户端

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

这篇关于从WCT RESTful服务消费的FileStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆