Reputation: 907
EDIT: I've update with a controller to get stream resources, but some how it still doesn't work
public class ResourcesController : Controller
{
public string Directory = @"D:\Desktop\MusicDatabase\Image\";
[HttpGet]
[Route("Image/{type}/{id}")]
public HttpResponseMessage Image(string type, string id)
{
HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
var url = Path.Combine(Directory, type, id + ".jpg");
byte[] fileData = System.IO.File.ReadAllBytes(url);
MemoryStream ms = new MemoryStream(fileData);
httpResponseMessage.Content = new StreamContent(ms);
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
return httpResponseMessage;
}
}
It return a string, not the resoure file
When I debug, "url" variable is
"D:\\Desktop\\MusicDatabase\\Image\\song\\2.jpg"
old:
I have 2 project: 1 web api2 for the server, 1 WP8 app for the client.
the server contains db where I have link to external file resources
e.x a Song table with IMAGE column where I stored this picture in my Desktop
public partial class Song
{
public int Id { get; set; }
public string NAME { get; set; }
private string _IMAGE;
public string IMAGE
{
get
{
return Helper.Helper.ConcatDirectory(_IMAGE);
}
set
{
_IMAGE = value;
}
}
In my DB, I stored "IMAGE" column only by its index (like "Song\1.jpg"), then when I have some queries to sever, I return full URL by this
public class Helper
{
public static string Directory = "D:\\Desktop\\MusicDatabase\\";
public static string ConcatDirectory(string input)
{
return string.Concat(Directory, input);
}
}
When I tried a query in web browser , this URL will return file stored in my PC
But my WP8 app can't show this image (debug with emulator). While I don't want to store this resource in my project , so is there anyway to return these images here ? (Any diffirent file type later, like song, video ...)
Upvotes: 1
Views: 9078
Reputation: 2140
Please replace
httpResponseMessage.Content = new StreamContent(ms);
with
httpResponseMessage.Content = new ByteArrayContent(ms);
EDIT:
I really think you should try to understand the following advise from other fellow SO users:
1) Handling static images in ASP.NET WebAPI: https://stackoverflow.com/a/12559507/2310818.
Upvotes: 0