I’m pulling a simple jpg from the file system like so …
var objz = "C:\\projects\\Desert.jpg";
byte[] imageSampleData = ReadImageToBytes(objz);
return imageSampleData;
(read img to btyes implementation is below)
byte[] ReadImageToBytes(string sPath)
{
byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(sPath, FileMode.Open,
FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
return data;
}
Then I’m trying to return this from an ASPNET MVC ActionResult like so …
return File(byteArrayFromMethodAbove, "image/jpg");
Then on the client I’m trying to set the src of an image like so …
<script type="text/javascript">
$(document).ready(function () {
loadImage();
});
function loadImage() {
$.ajax({
type: "GET",
url: "http://localhost:49415/Home/About",
data: {},
dataType: "jpg",
success: function (datasrc) {
$('#fk').attr('src', datasrc);
}
});
}
</script>
But so far the image doesn’t appear to agree with what I’m doing client side (or the encoding is wrong altogether). Currently watching firebug I do see some crazy data as the src but it doesn’t appear to be correct.
I’ve found a ton of question/answers on the site but none of which seem to actually work
Any help would be much much appreciated (using MVC 2 and jQuery 1.4.1)
Use of jquery is way overkill here. Just set src=url and let the browser do the downloading.