-2

I want to download a file (css type) from a concatenated style string from the database.I want to return a new file created with the given stylestrings and return on http get request

4
  • 2
    Possible duplicate of How to return raw string with ApiController? Commented Dec 5, 2017 at 9:36
  • @markus that is for a normal string.i have checked all those posts and posted my question here.Thanks for your comment Commented Dec 5, 2017 at 11:01
  • Please read How to Ask and read your oneliner as someone who doesn't know what exactly you're trying to do. The question doesn't make any sense. What do you mean by "download"? Do you want to issue an HTTP request from Web API, or do you want to offer a file for download? And what exactly is a "style string"? How does that style string relate to the file? Commented Dec 5, 2017 at 11:52
  • @YashwanthChowdaryKata the general procedure to download raw text is the same whether your string contains CSS or not. Only the content type differs (for CSS: text/css). So I think the answers to the linked question are also valid for your question. Please provide some more details on how you get the string from the database and concatenate it. Maybe you can also use the stream from your sample. Commented Dec 5, 2017 at 12:20

2 Answers 2

0

Your return object is likely using one or both of the IDisposable objects, but disposing of them shortly you return (by way of the using statements).

Manage your memory differently - make calls to stream.Dispose() and httpResponseMessage.Dispose() manually where appropriate - maybe in your class destructor, though in my experience HttpResponseMessage doesn't need disposing as it's dealt with by the garbage collector once you've finished working with it. Example code:

``` byte[] textAsBytes = Encoding.Unicode.GetBytes(concatenatedStyles);

        using(MemoryStream stream = new MemoryStream(textAsBytes)) {
            var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
            httpResponseMessage.Content = new StreamContent(stream);
            httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
                FileName = "main-theme.scss"
            };
            httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/css");

            return ResponseMessage(httpResponseMessage);
        }

```

You might need to remove the using around the MemoryStream too, I'm not 100% sure as I can't compile your code on my system.

Sign up to request clarification or add additional context in comments.

Comments

-2
ResponseMessageResult responseMessageResult;

using (MemoryStream stream = new MemoryStream(textAsBytes))
using (HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK))
{
    httpResponseMessage.Content = new StreamContent(stream);
    httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = "main-theme.scss"
    };
    httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/css");

    responseMessageResult = ResponseMessage(httpResponseMessage);
}

return responseMessageResult;

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.