C#Web服务将存储为BLOB的PDF格式返回PDF格式

时间:2016-02-23 10:30:16

标签: c# web-services pdf

我是C#(和Visual Studio)的新手。到目前为止,我已成功部署了一个从本地MSSQL服务器读取数据的WEB服务,但我想用VS2010扩展它。

我有一个本地mySQL数据库,除其他数据外,还有一个存储PDF文件的BLOB列。我想阅读这个BLOB列并将结果从Web服务返回为PDF文件。该服务将从PHP调用,在类似的情况下,我能够毫不费力地呈现PDF。

到目前为止,谷歌搜索它,我能够读取数据并将其存储为本地文件。我想不将数据存储在一个文件中并将其存储在一个变量(哪种类型?)中,这样我就可以将此变量作为Web服务的回复发送。

谢谢!

(我有一个强大的PHP背景,但我发现一些困难'将这个背景翻译成VS)

3 个答案:

答案 0 :(得分:0)

首先 - 为什么不VS2015?社区版是免费的,并支持扩展。没有理由住在过去:)

对于您的问题,我认为您只缺少标题信息。

如果你把它作为mime类型添加到你的输出中,你应该是免费的:

即:

var pdfBytes = DocumentRepository.GetPdfAsByteArray(myPdf);
context.Response.StatusCode = (int) HttpStatusCode.OK;
context.Response.ContentType = "application/pdf";
context.Response.OutputStream.Write(pdfBytes, 0, pdfBytes.Length);

答案 1 :(得分:0)

你可以试试这个:

lsof -i -P |grep pid

答案 2 :(得分:0)

我设法解决了我的问题,结合了这里的一些信息和其他答案。通常,我想避免将BLOB中的PDF写为物理文件,然后打开并将其作为文件读取。谢谢你的帮助!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Configuration;
using MySql.Data.MySqlClient;
using System.IO;

public MySqlConnection myInit()
{
    return new MySqlConnection(
        "host=" + ConfigurationManager.AppSettings["mysqlHost"] +
        ";user=" + ConfigurationManager.AppSettings["mysqlUser"] +
        ";password=" + ConfigurationManager.AppSettings["mysqlPass"] +
        ";database=" + ConfigurationManager.AppSettings["mysqlDb"]);
}

public Dictionary<string,byte[]> getPDF(int pdfid)
{
    string query = "SELECT md5_string, data FROM pdf,pdf_view WHERE pdf.id=" + pdfid + " and pdf_view.id=pdf.id";
    MySqlConnection con = myInit();
    MySqlCommand cmd = new MySqlCommand(query, con);

    MemoryStream ms;
    BinaryWriter bw;                        // Streams the BLOB to the MemoryStream object.
    Dictionary<string, byte[]> result = new Dictionary<string, byte[]>();

    int bufferSize = 1000;                   // Size of the BLOB buffer.
    byte[] outbyte = new byte[bufferSize];  // The BLOB byte[] buffer to be filled by GetBytes.
    long retval;                            // The bytes returned from GetBytes.
    long startIndex = 0;

    con.Open();

    MySqlDataReader myReader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);

    while (myReader.Read())
    {
        ms = new MemoryStream();
        bw = new BinaryWriter(ms);

        string md5 = myReader.GetString(0);

        // Reset the starting byte for the new BLOB.
        startIndex = 0;

        // Read the bytes into outbyte[] and retain the number of bytes returned.
        retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);

        // Continue reading and writing while there are bytes beyond the size of the buffer.
        while (retval == bufferSize)
        {
            bw.Write(outbyte);
            bw.Flush();

            // Reposition the start index to the end of the last buffer and fill the buffer.
            startIndex += bufferSize;
            retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
        }

        // Write the remaining buffer.
        bw.Write(outbyte, 0, (int)retval - 1);
        bw.Flush();

        result.Add(md5, ms.ToArray());

        // Close the output stream.
        bw.Close();
        ms.Close();
    }

    // Close the reader and the connection.
    myReader.Close();
    con.Close();

    return result;
}

我和PHP一起使用它:

$o = new SoapClient('http://localhost:3153/IntranetConnect.svc?wsdl', array('trace' => 0, 'cache_wsdl' => WSDL_CACHE_NONE));
$pdf = $o->getPDF(array('pdfid' => 1109));

foreach($pdf->getPDFResult as $value) {
   header("Content-type: application/pdf");
   header('Content-disposition: filename='.$value->Key.'.pdf');
   echo $value->Value;
}