Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

controller's method return file web api ?

$
0
0

Hi,

I am build a web api cors, i have a <img src="http://localhost:89/api/file/{id}" />, you can see the src attribute which invoke a get method of FileController.

I tried to the code as below, it doesn't work. Please, give some resolve. Thanks !

public class FileController : ApiController
    {
        [HttpGet]
        [Route("api/file/{id}")]
        public HttpResponseMessage Get([FromUri] Int64 id)
        {
            var f = Repository.File.SingleOrDefault(x => x.Id == id);
            var fileManager = new FileManager();
            var fileStream = fileManager.Open(f.FileName);

            var response = new HttpResponseMessage();
            response.Content = new StreamContent(fileStream); // this file stream will be closed by lower layers of web api for you once the response is completed.
            response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentType = new MediaTypeHeaderValue(f.FileType);

            return response;
        }

    }


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>