How To Return PDF/Binary Data From a Database In a WCF

爱⌒轻易说出口 提交于 2019-12-25 02:50:40

问题


I have a WCF-REST service that returns data in JSON format, that reads from a database in SQLSERVER... To return simple data, I don't have any problem.

Now, I want to return,a PDF file, that is at the database in varbinary(max) field.
What is the correct way to return the PDF data in a WCF Rest Service?

[EDITED] This is how It was suggested. This is the class:

    [WebGet(UriTemplate = "/documents/{id}")]
    public ActionResult GetDocument(int id)
    {
        using (var context = new CorrespondenceDataContext())
        {
            var item = context.DocumentsPDFs.Find(id);
            return File(item.Document, "application/pdf", "Document-" + id);
        }
    }

Now I already did it exactly like the suggestion, but I guess it's not compatible with my project (I'm a little bit new on this)...
First error: The type or namespace name "ActionResult" could not to be found
Second error: System.Data.Linq.Table does not contain a definition for Find
Third error: System.IO.File is a type but is used like a variable

I tried to add System.Web.MVC, but it does not appears. My project is WCF Service Application...


回答1:


    [WebGet(UriTemplate = "/documents/{id}")]
    public ActionResult GetDocument(int id)
    {
        using(var context = new CorrespondenceDataContext())
        {
            var item = context.DocumentsPDFs.Find(id);
            return File(item.Document, "application/pdf", "Document-" + id);
        }
    }


来源:https://stackoverflow.com/questions/16027117/how-to-return-pdf-binary-data-from-a-database-in-a-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!