download blob file from sql using c# asp.net and do not displaying in their related viewer applications?

1.9k Views Asked by At

I have a gridview displaying records. The grid view has a linkbutton which I want to use to download a blob file wich is saved in the same table as where the gridview is loading its data from. the file is downloading very well but the problem is that when i open the downloaded file it does not showing anythng in their related viewer applications mean if i download a pdf file and after downloading when i open in adobe reader file opens well in adobe reader complete pages are showing but blank no data displaying same in jpg, ppt, xlx etc. , here is my code

string[] commandArgument = e.CommandArgument.ToString().Split('|');
            hfResourcesdocumentId.Value = commandArgument[0];
            hfBlobId.Value = commandArgument[1];

            if (e.CommandName == "ViewFile")
            {

                GetBlob(hfBlobId.Value);
             }



protected void GetBlob(string blobId)
    {
        string url = "https://api.truevault.com/v1/vaults/" + vaultId + "/blobs/" + blobId;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.ContentType = "application/json";
        request.Accept = "*/*";
        request.Method = WebRequestMethods.Http.Get;
        request.Headers.Add("Authorization", "Basic " +               Convert.ToBase64String(Encoding.ASCII.GetBytes(apiKey + ":")));

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        StreamReader streamReader = new StreamReader(response.GetResponseStream());
        string html = streamReader.ReadToEnd();
        response.Close();
        streamReader.Close();
        string file = Convert.ToString(response.Headers["Content-Disposition"]);
        string[] str = file.Split('=');
        string filename = str[1];
        Byte[] bytes = Encoding.UTF8.GetBytes(html);
        Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/jpg/png/gif/pdf/ppt/xlx/docx";
        Response.AddHeader("content-disposition", "attachment;filename="+filename);
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
}
2

There are 2 best solutions below

0
On

Try using BinaryReader instead of StreamReader. Then take out the line

Byte[] bytes = Encoding.UTF8.GetBytes(html);

When requesting a BLOB read, TrueVault sends back binary data, no special encoding.

See: StreamReader vs BinaryReader?

0
On
protected void GetBlob(string blobId)
    {
string url = "https://api.truevault.com/v1/vaults/" + vaultId + "/blobs/" + blobId;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.ContentType = "application/json";
        request.Accept = "*/*";
        request.Method = WebRequestMethods.Http.Get;
        request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(apiKey + ":")));

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        BinaryReader streamReader = new BinaryReader(response.GetResponseStream());
        //string html = Convert.ToString(streamReader.ReadInt32());
        const int bufferSize = 4096;
        byte[] test;
        using (var ms = new MemoryStream())
        {
            byte[] buffer = new byte[bufferSize];
            int count;
            while ((count = streamReader.Read(buffer, 0, buffer.Length)) != 0)
                ms.Write(buffer, 0, count);
            test = ms.ToArray();
        }


        string file = Convert.ToString(response.Headers["Content-Disposition"]);
        string[] str = file.Split('=');
        string filename = str[1];
        //  Byte[] bytes = streamReader.ReadBytes(int.MaxValue);
        //Byte[] bytes = Encoding.UTF8.GetBytes(html);
        Response.Buffer = true;
        Response.Charset = "";
        response.Close();
        streamReader.Close();
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/jpg/png/gif/pdf/ppt/xlx/docx";
        Response.AddHeader("content-disposition", "attachment;filename=" + filename);

        Response.BinaryWrite(test);
        Response.Flush();
        Response.End();
}