0

我有一个显示记录的gridview。网格视图有一个链接按钮,我想用它来下载一个 blob 文件,该文件保存在与网格视图加载其数据的位置相同的表中。文件下载得很好,但问题是当我打开下载的文件时,它在其相关的查看器应用程序中没有显示任何内容,这意味着如果我下载 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();
}
4

2 回答 2

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();
}
于 2015-01-28T09:55:06.303 回答
0

尝试使用 BinaryReader 而不是 StreamReader。然后取出线

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

当请求读取 BLOB 时,TrueVault 会发回二进制数据,没有特殊编码。

请参阅:StreamReader 与 BinaryReader?

于 2015-01-05T23:51:05.863 回答