I need to download a file located in the App_Data folder of my project, all the permissions are granted, I have the following code to download an .xlsx file by calling a WebService in C #, the result of the process is not error, but it does not download the file in the browser and in the preview when inspecting the process shows unknown characters.
AJAX Jquery code:
function DOWNLOAD_EXCEL_SERVICES() {
$.ajax({
type: 'POST',
url: '../WebServices/getJSON.asmx/DOWNLOAD_EXCEL_SERVICES',
contentType: 'application/x-msexcel',
success: OnSuccess,
error: function (xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
function OnSuccess() {
console.log("donloading-services");
}
}
WebService C # code:
[WebMethod]
public void DOWNLOAD_EXCEL_SERVICES()
{
var path = Server.MapPath("~/App_Data//");
string filename = "SERVICES.xlsx";
System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
byte[] Content = File.ReadAllBytes(path + "SERVICES.xlsx");
Response.ContentType = "application/x-msexcel";
Response.AddHeader("content-disposition", "attachment; filename=\"" + filename + "\"");
Response.BufferOutput = true;
Response.OutputStream.Write(Content, 0, Content.Length);
Response.Flush();
}
Process Output inspecting in Chrome:
Additionally, if I use Response.End()
instead of Response.flush()
simply mark "Proces was being aborted", even if I use a try-catch structure I get the same result, it does not release the file.