last day (14 days later) » 

1:44 AM
1
A: responding with a file to a get HTTP request

MostafizYou can do by public FileContentResult GetAttachment(Guid noteGuid) { byte[] content = Convert.FromBase64String(retrievedAnnotation.DocumentBody); FileContentResult result = new FileContentResult(content, "application/octet-stream") { FileDownloadName = "your_file_name" ...

 
thanks so much, i've changed the question just a bit, im wondering how do i respond with the attachment WITHOUT saving it locally, i want to just stream that byte arraay
also, i'm not going to know the file type, im only going to know the extension of the fiel
 
ok check out updated answer
 
genius thank you very much, im trying it right now.
i got this response: { "FileContents": "eWVz", "ContentType": "application/octet-stream", "FileDownloadName": "blabsssg4rrxt.txt" }
the filedownloadname is correct, however the filecontents should just be "yes"
 
didn't understand, can you please make me more clear ?
` "FileContents": "eWVz"` this part ?
 
thank you again for your help
the file contents are simply "yes"
but the response is showing "ewvz"
 
1:45 AM
you are welcome
what it show after download the file ?
 
im using postman to test
it doesnt download anything
im actually not sure that i am testing it correctly
 
ok there is another solution I am updating answer with it, lets try it
 
ok
maybe if i specify the content type here
return new FileContentResult(content, "application/octet-stream")
{
FileDownloadName = retrievedAnnotation.FileName
};
then maybe it will know how to interpret it
i gess not, i am wrong
{
"FileContents": "eWVz",
"ContentType": "text/plain",
"FileDownloadName": "blabsssg4rrxt.txt"
}
i did set the contenttype to text plain and still got the same response
 
you can try and check, I have posted another solution in update lets check and also check Convert.FromBase64String(retrievedAnnotation.DocumentBody); portion returning correct bye
*byte
 
1:53 AM
yeah maybe Convert.FromBase64String(retrievedAnnotation.DocumentBody); function returning wrong byte
 
error on this line return File(content, "text/plain", "filename.txt");
Severity Code Description Project File Line Suppression State
Error CS1955 Non-invocable member 'File' cannot be used like a method. Omnia360.Api.Service F:\tfs\Omnia360\DEV\Source\PRODUCTAPI\Omnia360.Api.Service\NoteServices\NoteService.cs 160 Active
 
ok add this System.Web.MVC before file like System.Web.MVC.File
this File method is from System.Web.MVC no IO so that error come
 
u knwo what
i am using ApiController
not MVC
my fault
so i think i will try this
50
A: How to return a file (FileContentResult) in ASP.NET WebAPI

BlaiseHere is what I found: Instead of returning StreamContent as the Content, I can make it work with ByteArrayContent. [HttpGet] public HttpResponseMessage Generate() { var stream = new MemoryStream(); // processing the stream. var result = new HttpResponseMessage(HttpStatusCode.OK) ...

 
yes if you use WebApi then this is the right way
 
how do i add the file contents to it though i do not understand?
 
2:06 AM
ok let me check
 
do i say var stream = Convert.FromBase64String(retrievedAnnotation.DocumentBody);
??
 
ok I am updating answer with this solution
answer updated with webapi
 
Severity Code Description Project File Line Suppression State
Error CS0029 Cannot implicitly convert type 'byte[]' to 'System.Net.Http.HttpContent' Omnia360.Api.Service F:\tfs\Omnia360\DEV\Source\PRODUCTAPI\Omnia360.Api.Service\NoteServices\NoteService.cs 158 Active
on this line
        response.Content = content;
 
sorry my mistake replace with this response.Content = new ByteArrayContent(content);
check answer I have updated
 
BEAUTIFUL
now if my content is jpeg or png or other, then how do i identify which contnet-type it is?
would i use something like this?
extension
158
Q: Get MIME type from filename extension

acidzombie24How can I get the MIME type from a file extension?

 
2:21 AM
yes you can get MIME type this way
 
is it important to specify mime type?
why do i need to specify it? so that the client knows how to interpret it?
 
actually through http communication you have to specify MIME type to safely communicate client and server other that they didn't know each other content. The purpose of the Content-Type field is to describe the data contained in the body fully enough that the receiving user agent can pick an appropriate agent or mechanism to present the data to the user, or otherwise deal with the data in an appropriate manner.
 
thank you very much
 
you are most welcome
 

last day (14 days later) »