Reputation: 7092
I have a c# controller that returns a status file for the system. It prompts the user to open or download the file. The return type for the controller is actually System.Net.Http.HttpResponseMessage.
I set up the route like this: /api/logging/status/{reqID}
If I goto the route directly in my web browser, http://mysite//api/logging/status/12345678, it works fine.
How can I get the URL to the file via jquery so that I can put that url in an achor tag like:
<a href="http://path.to.my.status.file/status.txt">Download Status</a>
Here is the controller:
[Route("/api/logging/status/{reqID}")]
public IHttpActionResult GetStatus(Int32 reqID)
{
Stream stream;
stream = new MemoryStream();
var reqStatus = serializer.SerializeToString(req);
var bytes = System.Text.Encoding.UTF8.GetBytes(reqStatus);
stream.Write(bytes, 0, bytes.Length);
stream.Seek(0, SeekOrigin.Begin);
}
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.Add("Content-Disposition", String.Format("attachment; filename=\"{0}\"", "status.txt"));
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
return ResponseMessage(response);
}
Is there something I'm missing?
Thanks!
Upvotes: 0
Views: 375
Reputation: 147
@999cm999,
It seems to me that you need another service method to serve the path to the txt file. You only have a method that serves the file itself as a download. That's how the browser example works.
However, if you want to insert the path to the txt file (http://path.to.my.status.file/status.txt) in an anchor tag of your HTML page, the method that you have does not fit. Create another method that returns the path to the file as a string. Then you can grab that and put it in the HREF attribute of your hyperlink element using jQuery or your favorite JS approach.
Let me know of your findings.
Upvotes: 1
Reputation: 773
Have you already tried the following?
$.get('/api/logging/status/12345678', function (data) {
console.log(data);
});
Upvotes: 1