我有一个显示记录的gridview。网格视图有一个链接按钮,我想用它来下载一个blob文件,它保存在gridview加载其数据的同一个表中。 该文件下载得非常好,但问题是,当我打开下载的文件时,它没有显示任何相关的查看器应用程序意味着如果我下载pdf文件和下载后我打开adobe reader文件打开好adobe阅读器完成页面显示但空白没有数据在jpg,ppt,xlx等中显示相同,这是我的代码
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();
}
答案 0 :(得分:0)
尝试使用BinaryReader而不是StreamReader。然后取出
行Byte[] bytes = Encoding.UTF8.GetBytes(html);
请求BLOB读取时,TrueVault会发送回二进制数据,而不是特殊编码。
答案 1 :(得分: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();
}