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
-
2Possible duplicate of How to return raw string with ApiController?Markus– Markus2017-12-05 09:36:33 +00:00Commented 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 commentYashwanth Kata– Yashwanth Kata2017-12-05 11:01:07 +00:00Commented 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?CodeCaster– CodeCaster2017-12-05 11:52:22 +00:00Commented 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.Markus– Markus2017-12-05 12:20:43 +00:00Commented Dec 5, 2017 at 12:20
2 Answers
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.
Comments
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;