I am working on an AJAX function that calls a server-side method which in turn calls an API and returns a byte array. My goal is to convert this byte array into a PDF file and then download the file. Here is the code I have:
AJAX function:
function ObtainFile(fileNumber) {
debugger;
$.ajax({
type: 'POST',
url: '/De.aspx/FindFile',
data: JSON.stringify({ fileNumber: fileNumber }),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
debugger;
if (msg.d != null) {
window.open();
}
},
error: function () {
}
});
}
Code behind method:
[System.Web.Services.WebMethod]
public static byte[] FindFile(string fileNumber)
{
var response = new Serv.L.SeL((U)HttpContext.Current.Session["UT"]).ObtainFile(fileNumber);
if (response != null)
{
File.WriteAllBytes(@"~\Content\tmp\hello.pdf", response.F);
return response.F;
}
else
return null;
}
I attempted to use a relative path in the FindFile method, but faced difficulty with passing it into File.WriteAllBytes. I am unsure of how to automatically download the new PDF file or save it in the tmp folder within the project. Furthermore, I would like to open a new tab with the PDF file. While I can achieve this with an absolute path, it may not work if the project is deployed on another server. For example, it works on my local machine with a path like C:/User/Desktop.
Any suggestions or advice would be greatly appreciated. Thank you.