0

I have a web application in ASP.NET with C# and i am having an issue opening some PDF's.

The code looks up the selected ID and pulls the pdf path associated with that ID. The path that is being returned is working, and is correct. For some reason, when I hit open in the browser it says the file is corrupted and cannot be repaired, but when I open it in adobe everything is perfect.

Here is my code:

string id;
string path;

DataTable dt = Session["UnmatchedItems"] as DataTable;

ASPxButton button = (ASPxButton)sender;

id = button.CommandName;

DataView dv = new DataView(dt);

dv.RowFilter = "id = " + id;

path = dv[0][2].ToString();

Response.ClearHeaders();

Response.ContentType = "application/pdf";

Response.AddHeader("Content-Disposition", "attachment; filename=" + path);

Response.WriteFile(path);

Not sure what is going on or why it won't open...

If I forgot anything or you need more info, let me know!

4

1 回答 1

1

这对我有用:

byte[] fileContents = System.IO.File.ReadAllBytes(Server.MapPath("~/" + pathToFile));
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + filename);
Response.OutputStream.Write(fileContents, 0, fileContents.Length);
于 2013-09-25T15:47:51.793 回答