Ich verwende eine Bibliothek zum Konvertieren von HTML in PDF. Nach dem Konvertieren in PDF Wie kann ich diese konvertierte PDF-Datei im Anwendungsordner in einem Controller speichern? Hier ist der Code:Wie speichert man Dateien/Daten im Anwendungsordner in MVC?
public ActionResult ABC(ResearchProposal model)
{
ViewDataDictionary viewData = new ViewDataDictionary(model);
// transmit the posted data to view
viewData["MyModel"] = model;
StringWriter stringWriter = new StringWriter();
// Render the Index view in a HTML string
ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, "ABC", null);
ViewContext viewContext = new ViewContext(
ControllerContext,
viewResult.View,
viewData,
new TempDataDictionary(),
stringWriter
);
viewResult.View.Render(viewContext, stringWriter);
// Get the view HTML string
string htmlToConvert = stringWriter.ToString();
// Get the base URL
String currentPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
String baseUrl = currentPageUrl.Substring(0, currentPageUrl.Length - "Reports/ABC".Length);
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the converter in demo mode
htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";
// Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed
// Set this property to 0 if you don't need to wait for such asynchcronous operations to finish
htmlToPdfConverter.ConversionDelay = 2;
// Convert the HTML string to a PDF document in a memory buffer
byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlToConvert, baseUrl);
// Send the PDF file to browser
FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
fileResult.FileDownloadName = "Convert_Current_Page.pdf";
return fileresult;
}
aber es gibt Fehler bei file.writeallbytes(); 'System.Web.Mvc.Controller.File (byte [], string)' ist eine 'Methode', die im gegebenen Kontext nicht gültig ist. –
@MuhammadSajidGhufran Oh, sorry, du musst 'System.IO' Namespace verwenden. So ist es tatsächlich, 'System.IO.File.WriteAllBytes()'. –
ja. Ich habe es getan .. aber jetzt eine Ausnahme auslösen bei: System.IO.File.WriteAllBytes ("~ G/Initial try/Inhalt/Daten /" + model.Name, outPdfBuffer); Ein Teil des Pfads 'C: \ Programme \ Gemeinsame Dateien \ Microsoft Shared \ DevServer \ 10.0 \ ~ G \ Initial try \ Content \ data \' konnte nicht gefunden werden. –