0

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!

10
  • 1
    Please post your controller code. Commented Aug 20, 2015 at 14:26
  • 1
    When you debug the server-side code, how does it fail? Also, why are you using a different URL structure in the AJAX requests than in the manual request? (In the AJAX you're using a query string parameter instead of putting the ID directly in the route.) Commented Aug 20, 2015 at 14:31
  • 1
    You can't download files using an ajax request. Commented Aug 20, 2015 at 14:32
  • 1
    Also, what code is retrieving the value for reqID from the US to send to the server? Commented Aug 20, 2015 at 14:33
  • 1
    @999cm999 There are libraries that fake it e.g. johnculviner.com/… Commented Aug 20, 2015 at 14:38

2 Answers 2

1

Have you already tried the following?

$.get('/api/logging/status/12345678', function (data) {
        console.log(data);
    });
Sign up to request clarification or add additional context in comments.

Comments

1

@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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.