0

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
  • Just a disclaimer: Using a C#/asp.net application to interface with TrueVault in this manner generally means that you are not HIPAA compliant. PHI must never pass through a non-HIPAA compliant server to be considered HIPAA compliant. Commented Jan 2, 2015 at 17:28
  • i think this could be satisfactory answer, because i'm using trial account so for Commented Jan 5, 2015 at 7:49

2 Answers 2

0

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?

Sign up to request clarification or add additional context in comments.

Comments

0
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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.