After spending several hours searching and struggling to prompt a download of a text file from the server to the User's machine, I've decided to seek help here. The function is located in an ApiController.
public class LogViewerController : ApiController
Below is the code for the function:
public void Get()
{
try
{
string path = @"C:\TestDocument.txt";
var context = HttpContext.Current;
context.Response.Clear();
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename="Test");
context.Response.WriteFile(path);
context.Response.End();
}
catch (Exception e)
{
//
}
}
The goal is to retrieve the file at the specified path and prompt the user to download/save it. However, instead of downloading, the file content is being printed out in '#textField'.
Here is the Javascript/ajax code:
$.ajax({
cache: false,
type: 'GET',
url: '@ViewBag.servicesUrl/LogViewer',
datatype: 'JSON',
success: function (retdata) {
$('#textField').html(retdata);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//
},
});
I am puzzled by what might be causing this issue. Could it be related to the fact that it's within a GET request?
This is my first time posting a question here. Please let me know if further information is needed. Thank you for any assistance!