Using the jsPDF plugin, I am creating a .pdf file and generating a download based on a button click. I would like to save the file onto my server i
Using Asp.net/C# and jQuery: Based on @mahmoud hemdan answer, i did for my requirement. I did like below:
Javascript:
function createPDF(){
var doc = new jsPDF('landscape');
var u = $("#myCanvas").toDataURL("image/png", 1.0);
doc.addImage(u, 'JPEG', 20, 20, 250, 150);
var base64pdf = btoa(doc.output());
$.ajax({
url: 'ChartPDF.aspx?pdfinput=1',
type: 'post',
async: false,
contentType: 'application/json; charset=utf-8',
data: base64pdf,
dataType: 'json'
});
}
ChartPDF.aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
var pdfinput= Request.QueryString["pdfinput"];
if (pdfinput!= null)
{
var jsonString = string.Empty;
HttpContext.Current.Request.InputStream.Position = 0;
using (var inputStream = new StreamReader(Request.InputStream))
{
jsonString = inputStream.ReadToEnd();
}
var byteArray = Convert.FromBase64String(jsonString);
//Get your desired path for saving file
File.WriteAllBytes(@"C:\ReportPDFs\Test.pdf", byteArray);
}
}
I called up the page without any querystring parameter and the page creates the graph(Code not given for drawing the graph as it is not part of the qs.) and after that it calles createPDF() function which result in reloading the page and saving the graph as pdf file on server. Later i use the file from path.