![](/img/trans.png)
[英]download blob file from sql using c# asp.net and present a save dialog to user
[英]download blob file from sql using c# asp.net and do not displaying in their related viewer applications?
我有一個顯示記錄的gridview。 網格視圖具有一個鏈接按鈕,我想使用該按鈕下載blob文件,但該文件保存在與gridview從中加載其數據的位置相同的表中。 該文件下載得非常好,但是問題是,當我打開下載的文件時,在它們相關的查看器應用程序中沒有顯示任何內容,意味着如果我下載了pdf文件,並且在下載后在Adobe Reader中打開時文件在Adobe Reader中完全打開了頁面正在顯示,但是空白,沒有數據在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();
}
嘗試使用BinaryReader而不是StreamReader。 然后取出線
Byte[] bytes = Encoding.UTF8.GetBytes(html);
當請求讀取BLOB時,TrueVault發送回二進制數據,沒有特殊編碼。
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.