string _imgpath1 = WebConfigurationManager.AppSettings["IMGPATH1"].ToString();
string outputPath = Server.MapPath(Request.ApplicationPath + "/output");
string file = Guid.NewGuid().ToString() + ".pdf";
string desFile = Path.Combine(outputPath, file);
string relativepath = "output/" + file;
//转义
//System.Text.Encoding gb2312 = System.Text.Encoding.GetEncoding("gb2312");
//string encodeFilePath = _imgpath1 + HttpUtility.UrlEncode(_imgpath2 + genre, gb2312).Replace("%2f", "/").Replace("%5c", "/").Replace("+", "%20");
private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
{
int bytesRead = ;
byte[] buffer = new byte[];
FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, true);
request.Method = WebRequestMethods.Ftp.DownloadFile;
Stream reader = request.GetResponse().GetResponseStream();
FileStream fileStream = new FileStream(localDestinationFilePath, FileMode.Create);
while (true)
{
bytesRead = reader.Read(buffer, , buffer.Length);
if (bytesRead == )
break;
fileStream.Write(buffer, , bytesRead);
}
fileStream.Close();
}
private FtpWebRequest CreateFtpWebRequest(string ftpDirectoryPath, string userName, string password, bool keepAlive = false)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpDirectoryPath));
//Set proxy to null. Under current configuration if this option is not set then the proxy that is used will get an html response from the web content gateway (firewall monitoring system)
request.Proxy = null;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = keepAlive;
request.Credentials = new NetworkCredential(userName, password);
return request;
}
try
{
string template = Server.MapPath(Request.ApplicationPath + "/reporttemplate/国有土地使用证.mrt");
string outputPath = Server.MapPath(Request.ApplicationPath + "/output");
//删除过期的临时PDF文件
List<FileInfo> toDeleteFiles = new List<FileInfo>();
Directory.GetFiles(outputPath, "*.pdf").ToList().ForEach(i => {
if ((new FileInfo(i)).CreationTime < DateTime.Now)
toDeleteFiles.Add(new FileInfo(i));
});
Task task = new Task(() =>
{
for (int i = toDeleteFiles.Count - ; i >= ; i--)
{ toDeleteFiles[i].Delete(); }
});
task.Start();
StiReport report = new StiReport();
report.Load(template);
report.RegBusinessObject("land", "cer", cer);
report.Compile();
report.Render(true);
string file = Guid.NewGuid().ToString() + ".pdf";
string relativepath = "/output/" + file;
string fullpath = Path.Combine(outputPath, file);
report.ExportDocument(StiExportFormat.Pdf, fullpath);
return View(new PdfEntity() { Filename = fullpath, RelativeFilename = relativepath });
}
catch (Exception ex)
{
return View("Error", new HandleErrorInfo(ex, "CerController转换为PDF文件时发生异常", "Index"));
}