简体   繁体   English

BinaryWriter.Write未创建.pdf文件

[英]BinaryWriter.Write is not creating the .pdf file

I am using this function to write a report into a pdf file and send it as an attachment in a email. 我正在使用此功能将报告写入pdf文件,并将其作为附件发送到电子邮件中。

string strNovaQueryString = string.Empty;
string pathFile = "";

string[] fields;
string[] values;

fields = ParamRelatorio.Split('|');

foreach (string key in fields)
{
    string[] param= key.Split(new char[] { '=' });
    strNovaQueryString += param[0] + "=" + param[1] + "&";
}

if (!string.IsNullOrEmpty(strNovaQueryString))
    strNovaQueryString = strNovaQueryString.TrimEnd('&');

string url = reportURL + "/ViewReport.aspx?" + strNovaQueryString;

string userName = user;
string password = pass;
string strPostData = String.Format("user={0}&pass={1}", userName, password);
byte[] postData = Encoding.ASCII.GetBytes(strPostData);

System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
req.ContentLength = postData.Length;

System.IO.Stream outputStream = req.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();

System.Net.HttpWebResponse rep = (System.Net.HttpWebResponse)req.GetResponse();
System.IO.Stream str = rep.GetResponseStream();
string contentType = rep.ContentType;

string fileType = "";

if (contentType != null)
{
    string[] splitString = contentType.Split(';');
    fileType = splitString[0];
}

if (fileType != null && fileType.ToLower() == "application/pdf")
{

    byte[] buffer = new byte[8192];

    int bytesRead = str.Read(buffer, 0, 8192);

    while (bytesRead > 0)
    {
        byte[] buffer2 = new byte[bytesRead];
        System.Buffer.BlockCopy(buffer, 0, buffer2, 0, bytesRead);

        pathFile = attPath+ "reportName" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";

        BinaryWriter binaryWriter = new BinaryWriter(File.Open(pathFile, FileMode.Create));
        binaryWriter.Write(buffer2);
        binaryWriter.Close();

        bytesRead = str.Read(buffer, 0, 8192);
    }

}
return pathFile;

It is saving a pdf file inside the path that I want (something like "C://Documents//Att"), but the pdf file is empty. 它将pdf文件保存在我想要的路径内(例如“ C:// Documents // Att”),但是pdf文件为空。 The email is being sent but the pdf is empty. 电子邮件正在发送,但pdf为空。 I think that binaryWriter.Write(bytesRead); 我认为binaryWriter.Write(bytesRead); is not working as expected, or the variable is empty. 不能按预期运行,或者变量为空。

Any suggestions? 有什么建议么?

Try using System.IO.File.WriteAllBytes(string path, byte[] bytes) instead of BinaryWriter . 尝试使用System.IO.File.WriteAllBytes(string path, byte[] bytes)代替BinaryWriter Its much simpler. 它要简单得多。 Check out the MSDN article here: 在此处查看MSDN文章:

https://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes%28v=vs.110%29.aspx

You dont' call to Stream.Flush, but Close stream (Close doesn't guarantee call to Flush) 您没有调用Stream.Flush,但是关闭了流(关闭并不能保证调用Flush)

And always use using(var stream= ... ) - because you must ensure that file will be unblocked. 并始终使用using(var stream = ...)-因为您必须确保该文件将被取消阻止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM