1

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

enter image description here

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

enter image description here

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 ...)

user3448806
  • 897
  • 10
  • 22
  • `IMAGE` is a string, containing the path to the image. You can't just send that to the phone and have the image appear. You need to read the contents of the image file and send that binary data in the response. – David Arno May 23 '15 at 08:34
  • Please have a look at this solution: http://stackoverflow.com/questions/13001588/output-image-using-web-api-httpresponsemessage. This is how you will be able to read the image from your phone. – Parth Shah May 23 '15 at 08:44
  • @ParthShah : I've updated the question, still can't get it work, I'm quite newbie in server app – user3448806 May 23 '15 at 10:09

1 Answers1

0

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.

Community
  • 1
  • 1
Parth Shah
  • 2,060
  • 1
  • 23
  • 34