Return image link with webservice

  • Thread starter Thread starter zion
  • Start date Start date
Z

zion

Hello,

How can I return image link with webservice that I could see it in web page?
The image is on my hard disk and <img src="c:\pictures\test.jpg" /> does not
work.
If I use <img src=http://My comuter/Virtual directory/test.jpg /> it's
working but I can't use this because the image path is in DB with phisycal
location.

Thanks
 
zion formulated the question :
Hello,

How can I return image link with webservice that I could see it in web page?
The image is on my hard disk and <img src="c:\pictures\test.jpg" /> does not
work.
If I use <img src=http://My comuter/Virtual directory/test.jpg /> it's
working but I can't use this because the image path is in DB with phisycal
location.

Thanks

The C:\ path won't work as that will always point to the local C: drive
for the browser, and that's not where your images are (and the
browser-machine might not even *have* a C: drive - unix doesn't!).

If you just want to return a link to the image from the webservice, add
a handler (ashx) to the webservice site that returns the image, based
on some image-id.
The URL returned from the webservice would then be something like
http://My Computer/theSite/ImageHandler.ashx?id=1234
This would look up the local path in your db, find the image-file and
return it using Response.WriteFile(..).

Hans Kesting
 
zion explained :
Thank you for reply.
Do you have any example for this solution?

public class ImageHandler : System.Web.IHttpHandler
{
// required by interface IHttpHandler
public bool IsReusable
{
get { return true; }
}

public void ProcessRequest(System.Web.HttpContext context)
{
int id = Int32.Parse(context.Request["id"]);
string filename = GetFilenameFromDatabase(id);
// need to implement this
// finds the full filename based on the supplied "id"

context.Response.ContentType = GetMimeTypeFromFilename(filename);
// need to implement this
// sets mimetype such as "image/jpeg"

context.Response.WriteFile(filename);
}
}


and in web.config, under <system.web>
<httpHandlers>
<add verb="GET" path="image.ashx" type="ImageHandler" />
</httpHandlers>

Note: the "type" value is the full classname (including namespace) of
your handler-class, optionally followed by a comma and the name of the
assembly it is defined in.



You can use it like <img src="http://server/image.ashx?id=1234">

Hans Kesting
 
Back
Top