在C#中使用SaveOrOpenFileDialog下载pdf文件 [英] download pdf file using SaveOrOpenFileDialog in c#
问题描述
我正在开发Web应用程序,
我使用Visual Stadio 2008做了一个水晶报告,可以生成pdf文件,当以pdf格式打开该报告时,它在与我的asp页面相同的窗口中打开,我希望在单击报告时可以打开saveorOpenFile窗口,这样我就可以打开或保存该报告.
如果您知道如何在c#中执行SaveOrOpenFileDialog,请提供帮助
我尝试了它不起作用:
I''m developing a web application,
I did a crystal report using visual stadio 2008 which can generate pdf file, when open that report in pdf it is open in the same window as my asp pages, i want that when click on report it can open saveorOpenFile window so that i can open or save that report.
please helps if you know how to do SaveOrOpenFileDialog in c#
I tried this it don''t work:
string str = @"~/Reports/ReportViewing.aspx?report=rptReservationByOffice&StartDate=" + txtStartDate.Text + "&EndDate=" + txtEndDate.Text;
String FileName = "FileName.txt";
String FilePath =str; //Replace this
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath);
response.Flush();
response.End();
推荐答案
private static MemoryStream downloadData(string url)
{
try
{
progress.Value = 0;
Application.DoEvents();
//Get a data stream from the url
WebRequest req = WebRequest.Create(url);
WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();
//Download in chuncks
Byte[] buffer = new Byte[1024];
//Get Total Size
Int32 dataLength = (Int32)response.ContentLength;
//With the total data we can set up our progress indicators
progress.Maximum = dataLength;
Application.DoEvents();
//Download to memory
//Note: adjust the streams here to download directly to the hard drive
MemoryStream memStream = new MemoryStream();
while (true)
{
//Try to read the data
Int32 bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
progress.Value = progress.Maximum;
Application.DoEvents();
break;
}
else
{
//Write the downloaded data
memStream.Write(buffer, 0, bytesRead);
if (progress.Value + bytesRead <= progress.Maximum)
{
progress.Value += bytesRead;
progress.Refresh();
Application.DoEvents();
}
}
}
stream.Close();
return memStream;
}
catch
{
//May not be connected to the internet
//Or the URL might not exist
throw new Exception("There was an error accessing the URL.");
}
}
现在要使用此
now to use this
string str = @"~/Reports/ReportViewing.aspx?report=rptReservationByOffice&StartDate=" + txtStartDate.Text + "&EndDate=" + txtEndDate.Text;
byte[] DownloadedData = DownloadData(str);
if (DownloadedData == null || DownloadedData.Length == 0)
return;
using (Stream Writer = File.Create(FilePath))
{
Writer.Write(DownloadedData, 0, DownloadedData.Length);
}
您可以使用
you can use
var str = "../Reports/ReportViewing.aspx?report=rptReservationByOffice&StartDate=" + document.getElementById("txtStartDate").value + "&EndDate=" + document.getElementById("txtEndDate").value;
window.open(str);
您可以从此处使用该窗口获取有关 window.open 的帮助. .open方法 [^ ]
谢谢,
Imdadhusen
you may take help for window.open from here Using the window.open method[^]
Thanks,
Imdadhusen
这篇关于在C#中使用SaveOrOpenFileDialog下载pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!