2008-12-06 40 views
9

Không chắc chắn chính xác cách để đặt câu hỏi này ... vì vậy các chỉnh sửa được hoan nghênh! Dù sao ... ở đây đi.Làm cách nào để đặt tên tệp khi phát trực tuyến Pdf trong trình duyệt?

Tôi hiện đang sử dụng Crystal Reports để tạo Pdf và chỉ truyền phát đầu ra cho người dùng. Mã của tôi trông giống như sau:

System.IO.MemoryStream stream = new System.IO.MemoryStream(); 

stream = (System.IO.MemoryStream)this.Report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat); 

this.Response.Clear(); 
this.Response.Buffer = true; 
this.Response.ContentType = "application/pdf"; 
this.Response.BinaryWrite(stream.ToArray()); 
this.Response.End(); 

Sau khi mã này chạy Pdf tới trình duyệt mở Acrobat Reader. Hoạt động tuyệt vời!

Vấn đề của tôi là khi người dùng cố gắng lưu tệp mặc định thành tên tệp thực tế ... trong trường hợp này mặc định là CrystalReportPage.pdf. Có anyway tôi có thể thiết lập này? Nếu vậy, làm thế nào?

Mọi trợ giúp sẽ được đánh giá cao.

Trả lời

17

Thông thường, thông qua header Content-Disposition - tức là

Content-Disposition: inline; filename=foo.pdf 

Vì vậy, chỉ cần sử dụng Headers.Add, hoặc AddHeader, hoặc bất cứ điều gì nó là, với:

response.Headers.Add ("Nội dung -Disposition "," inline; filename = foo.pdf ");

(inline là "hiển thị trong trình duyệt", đính kèm là "save as file")

7
System.IO.MemoryStream stream = new System.IO.MemoryStream(); 

stream = (System.IO.MemoryStream)this.Report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat); 

this.Response.Clear(); 
this.Response.Buffer = true; 
this.Response.ContentType = "application/pdf"; 
this.Response.AddHeader("Content-Disposition", "attachment; filename=\""+FILENAME+"\""); 
this.Response.BinaryWrite(stream.ToArray()); 
this.Response.End(); 
Các vấn đề liên quan