website using iTextSharp needs to save PDF on local machine C drive

冷暖自知 提交于 2019-12-13 06:01:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!