0

ASP.NET MVC 4 を使用して、SQL Server 2008 データベースから画像データ/非構造化データ (スキャンされたドキュメント) を取得し、Web ブラウザー経由でダウンロードする Web アプリを開発しています。Web ブラウザで表示するためのものではありませんが、クライアント マシンのデフォルト プログラムでダウンロードして表示する必要があります。私の主な問題は、データがダウンロードされた後、PC によってデータが認識されないことです。この問題を解決するには助けが必要です。

コードのスニペットを次に示します。

モデル

   public bool getData(string code)
   {

        SqlCommand cmd = new SqlCommand("spReadDocumentCode", con);
        cmd.CommandType = CommandType.StoredProcedure;
        bool state = false;

        cmd.Parameters.Add("@Code", SqlDbType.VarChar).Value =
          Convert.ToString(code);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        try
        {
            if (con.State == ConnectionState.Closed)
                con.Open();
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                image = (byte[])dt.Rows[0]["Data"];
                // I pass the data to a public MemoryStream variable.
                ms = new MemoryStream((byte[])dt.Rows[0]["Data"]); 
                state = true;
            }


            if (con.State == ConnectionState.Open)
                con.Close();

            return state;
        }
        catch
        {

            if (con.State == ConnectionState.Open)
                con.Close();

            return false;
        }

コントローラ

    [OutputCache(Duration=3600, VaryByParam="code", NoStore = true)]
    public ActionResult Document(string code)
    {

        MemoryStream img = null;
        if (Doc.getData(code))
        {
            img = Doc.getMemoryStream();

        }
        try
        {

            if (img == null)
            {
                return HttpNotFound();
            }
            // I send back an image file, however I want to upgrade the code 
            // to send back any type of scanned documents stored on the database.
            return new FileStreamResult(img, "image/png");  
        }
        catch 
        {
            return null;
        }

    }
4

1 に答える 1

2

データが破損している可能性があります。

データベースに保存されているバイト数が、ダウンロードしたファイルの同じバイト数と一致していることを確認できますか?もしそうなら、データベースに保存されている内容が本当に正しく、切り捨てられていないことを確信していますか?

データ型はVARBINARY(MAX)

于 2012-09-14T19:39:02.130 に答える