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();
}
Try using BinaryReader instead of StreamReader. Then take out the line
When requesting a BLOB read, TrueVault sends back binary data, no special encoding.
See: StreamReader vs BinaryReader?