如何在WCF中从数据库返回PDF/二进制数据 [英] How To Return PDF/Binary Data From a Database In a WCF
问题描述
我有一个WCF-REST服务,该服务以JSON格式返回数据,该数据从SQLSERVER中的数据库中读取...要返回简单数据,我没有任何问题.
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.
现在,我想返回一个PDF文件,该文件位于varbinary(max)字段中的数据库中.
在WCF Rest Service中返回PDF数据的正确方法是什么?
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?
这就是建议的方式.这是课程:
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);
}
}
现在我已经完全按照建议执行了,但是我想它与我的项目不兼容(我对此有些新意)...
第一个错误:找不到类型或名称空间名称"ActionResult"
第二个错误:System.Data.Linq.Table不包含Find
的定义
第三个错误:System.IO.File是一种类型,但是像变量一样使用
我试图添加System.Web.MVC,但它没有出现.我的项目是WCF服务应用程序...
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...
推荐答案
[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);
}
}
这篇关于如何在WCF中从数据库返回PDF/二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!