0

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...

user1600801
  • 269
  • 7
  • 25
  • It could very much help if you actually included the exception message and stack trace. Also I did NOT suggest what you have there. I was suggesting you use the EXACT code I gave you. DON'T wrap up your binary in some base64/JSON/XML wrapper. In short the correct way is to NOT use WCF Web (REST) API for this. – Aron Apr 17 '13 at 03:24
  • I also. I'm returning my data in JSON format, because I have another application, client application to read that data in that format... – user1600801 Apr 17 '13 at 16:57
  • Ah...Okay, I assumed you had a MVC 4 WebApi Controller with an EF backed storage. Sounds like you are using LinqToSQL with some kind of self hosted class. I'd need to know what the class that contains GetDocument inherits out of and what method you use to "register" it to the WPAService. – Aron Apr 17 '13 at 17:07

1 Answers1

0
    [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);
        }
    }
Aron
  • 15,464
  • 3
  • 31
  • 64