Pdf在本地渲染但不在dev上渲染

时间:2017-06-19 13:06:36

标签: c# asp.net web

我想在浏览器中查看pdf文件,以下代码在localhost上为我工作但在dev服务器上工作没有任何想法是错误的。

另外,我想在网格视图中点击链接按钮时在新窗口/标签中查看此pdf文件。

   protected void lnkBtn_grid_View_Command(object sender, CommandEventArgs e)
       {
        try
        {
            var fileName = e.CommandArgument.ToString();
            var normalTextByte=Encoding.UTF8.GetBytes(fileName);
            var encryptedFile = MachineKey.Encode(normalTextByte, MachineKeyProtection.All);
           Response.Redirect("~/statementfilesviewer.aspx?filename=" + Server.UrlEncode(encryptedFile));
        }
        catch (Exception ex)
        {
            Error.Log(ex);
        }
    }

   private void ReadPdfFile(string filename)
    {
        string keyName = "";
        try
        {
            string prefix = ConfigurationManager.AppSettings["Environment"] + "/StatementFiles/";
            keyName = prefix + SessionFacade.DbId + "/" + _intArtistId + "/" + filename;

            IAmazonS3 client;

            using (client = new AmazonS3Client())
            {
                var request = new GetObjectRequest
                {
                    BucketName = ConfigurationManager.AppSettings["BucketName"],
                    Key = keyName
                };

                string cwrFilePath;
                using (GetObjectResponse response = client.GetObject(request))
                {
                    cwrFilePath = @"~/PDFs/" + filename.Replace(" ", "_").Replace("-", "_");

                    if (File.Exists(Server.MapPath(cwrFilePath)))
                        File.Delete(Server.MapPath(cwrFilePath));

                    if (!File.Exists(Server.MapPath(cwrFilePath)))
                    {
                        response.WriteResponseStreamToFile(Server.MapPath(cwrFilePath));
                    }
                }

                if (File.Exists(Server.MapPath(cwrFilePath)))
                {
                    //Literal1.Text = " <object data=\"/PDFs/" + filename.Replace(" ", "_").Replace("-", "_") + "\" type=\"application/pdf\" height=\"600\" width=\"99%\" style=\"border: none;\"><embed src=\"/PDFs/" + filename.Replace(" ", "_").Replace("-", "_") + "\" type=\"application/pdf\" height=\"600\" width=\"99%\" style=\"border: none;\" /></object>";
                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("Content-Type", "application/pdf");
                    //Response.WriteFile(cwrFilePath);
                    Response.WriteFile(Server.MapPath(cwrFilePath));
                    Response.End();
                }
                else
                    Response.Write("File does not exists.  Please contact to support team.");
            }
        }
        catch (Exception)
        {
            Response.Write("File does not exists.  Please contact to support team.");
            EmailingDal.SendEmail("File does not exists on S3 server.", keyName);
        }
    }

1 个答案:

答案 0 :(得分:0)

User

这应该是在响应对象中写入文件,该对象是客户端变量。从代码中看,它看起来是在服务器端创建的文件,并在客户端浏览器中读取或显示。

您可以尝试使用StreamWriter直接在托管服务器上写入文件。

相关问题