ASP .Net Web API downloading images as binary
You can set the response content to a StreamContent object:
var fileStream = new FileStream(path, FileMode.Open); var resp = new HttpResponseMessage() { Content = new StreamContent(fileStream) }; // Find the MIME type string mimeType = _extensions[Path.GetExtension(path)]; resp.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);
While this has been marked as answered, it wasn't quite what I wanted, so I kept looking. Now that I've figured it out, here's what I've got:
public FileContentResult GetFile(string id){ byte[] fileContents; using (MemoryStream memoryStream = new MemoryStream()) { using (Bitmap image = new Bitmap(WebRequest.Create(myURL).GetResponse().GetResponseStream())) image.Save(memoryStream, ImageFormat.Jpeg); fileContents = memoryStream.ToArray(); } return new FileContentResult(fileContents, "image/jpg");}
Granted, that's for getting an image through a URL. If you just want to grab an image off the file server, I'd imagine you replace this line:
using (Bitmap image = new Bitmap(WebRequest.Create(myURL).GetResponse().GetResponseStream()))
With this:
using (Bitmap image = new Bitmap(myFilePath))
EDIT: Never mind, this is for regular MVC. for Web API, I have this:
public HttpResponseMessage Get(string id){ string fileName = string.Format("{0}.jpg", id); if (!FileProvider.Exists(fileName)) throw new HttpResponseException(HttpStatusCode.NotFound); FileStream fileStream = FileProvider.Open(fileName); HttpResponseMessage response = new HttpResponseMessage { Content = new StreamContent(fileStream) }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg"); response.Content.Headers.ContentLength = FileProvider.GetLength(fileName); return response;}
Which is quite similar to what OP has.
I did this exact thing. Here is my code:
if (!String.IsNullOrWhiteSpace(imageName)) { var savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.Combine(uploadPath, imageName)); var image = System.Drawing.Image.FromFile(savedFileName); if (ImageFormat.Jpeg.Equals(image.RawFormat)) { // JPEG using(var memoryStream = new MemoryStream()) { image.Save(memoryStream, ImageFormat.Jpeg); var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(memoryStream.ToArray()) }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); result.Content.Headers.ContentLength = memoryStream.Length; return result; } } else if (ImageFormat.Png.Equals(image.RawFormat)) { // PNG using (var memoryStream = new MemoryStream()) { image.Save(memoryStream, ImageFormat.Png); var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(memoryStream.ToArray()) }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); result.Content.Headers.ContentLength = memoryStream.Length; return result; } } else if (ImageFormat.Gif.Equals(image.RawFormat)) { // GIF using (var memoryStream = new MemoryStream()) { image.Save(memoryStream, ImageFormat.Gif); var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(memoryStream.ToArray()) }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/gif"); result.Content.Headers.ContentLength = memoryStream.Length; return result; } } }
And then on the client:
var client = new HttpClient(); var imageName = product.ImageUrl.Replace("~/Uploads/", ""); var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Properties.Settings.Default.DeviceMediaPath + "\\" + imageName); var response = client.GetAsync(apiUrl + "/Image?apiLoginId=" + apiLoginId + "&authorizationToken=" + authToken + "&imageName=" + product.ImageUrl.Replace("~/Uploads/","")).Result; if (response.IsSuccessStatusCode) { var data = response.Content.ReadAsByteArrayAsync().Result; using (var ms = new MemoryStream(data)) { using (var fs = File.Create(path)) { ms.CopyTo(fs); } } result = true; } else { result = false; break; }