问题
I'm trying to have iTextSharp create a PDF file of the web page and save it to the users computer C drive. This works in debug but when I publish it to the server files never get saved on the computer but the application is saying that it did save it. I think I need to set my path differently. This is in MVC4 C#.
var pdfDoc = new Document();
const string folderPath = "C:\\SG-ListingPDFs\\";
bool isExists = Directory.Exists(folderPath);
if (!isExists)
Directory.CreateDirectory(folderPath);
var writer = PdfWriter.GetInstance(pdfDoc, new FileStream("C:\\SG-ListingPDFs\\" + detailsViewModel.ListingNumber + ".pdf", FileMode.Create));
pdfDoc.Open();
//Code here
pdfDoc.Add(table);
TempData["SavedPdF"] = true;
pdfDoc.Close();
回答1:
No web server technology has the ability to save a file directly the users computer. What you would need to do is save the file to the server (which you are doing now) and then send a download to the users browser.
To do that in asp.net mvc..
public FileResult GetFile()
{
byte[] filebytes;
//load file data however you please
return File(filebytes, "application/pdf");
}
If you dont want want to save the file to the local disk, you could also generate your pdf in memory and send it straight out.
Create PDF in memory instead of physical file
来源:https://stackoverflow.com/questions/20245553/website-using-itextsharp-needs-to-save-pdf-on-local-machine-c-drive