如何连接code图像在浏览器中显示 [英] How to encode an image to display it in browser

查看:105
本文介绍了如何连接code图像在浏览器中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多保存在服务器端的文件夹中的图像,数据库有每个人的URL,因为在未经许可的客户端无法直接打开的图像。我的问题是我怎么能连接code中的图像浏览器从服务器上显示呢?

这是在$ C $词已经在行动结果搜索图像:

 公共FileResult SearchImage()
{

    VAR路径= @\\耶稣-PC \特拉\ IMAGENES \ SINGNOS DISTINTIVOS \ 0 \ 80HP23891268272.TIF;
    VAR IMG1 = System.IO.File.ReadAllBytes(路径);

    返回文件(IM1,图像/ JPG);
}
 

存在的形象,这是显示的内容:

我试着用 Convert.ToBase64String(IMG1),但我得到这个错误:

 与414(请求URI太长)状态的服务器响应
 

我是什么做错了吗?

更新:

我的坏,我没有写我在做什么,整个code。

这是没有提到的最后一部分是即时通讯使用AJAX调用图像。

当用户发送对于信息的请求,将有,将打开一个模式框的链接。在那个模式对话框是其中必须显示的图像,但图像将使用AJAX,因为页面不应该被重新加载加载。因此图像会被要求使用Ajax。这是我的code:

  $(DOC)。DBLCLICK(函数(){


$阿贾克斯({
    网址:炫魅/ SearchImage
    数据: ,
    类型:后,
    成功:功能(数据){
        ModalWindow.open();
        。的document.getElementById(img_1)SRC =数据;
    }

})
 

解决方案

考虑使用 FileStreamResult 而不是 FileResult 。这应该解决您的问题。 在code寿看起来是这样的:

 公共FileStreamResult SearchImage()
{

    VAR路径=
       @\\耶稣-PC \特拉\ IMAGENES \ SINGNOS DISTINTIVOS \ 0 \ 80HP23891268272.TIF;
    VAR FILESTREAM =新的FileStream(路径,FileMode.Open,FileAccess.Read);
    返回新FileStreamResult(FILESTREAM,为image / jpeg);
}
 

另一件事,我需要注意的是指定的MIME类型。在文件名是 TIF 但在code这是 JPEG

更新

为什么要使用AJAX的呢?你可以做这样的事情,而不是:

  $('#img_1)ATTR(SRC,炫魅/ SearchImage')。
 

I have alot of images saved in a folder on server side, the database has the url of each one and the client cannot open the images directly because of permisson. My problem is how can I encode the image to display it in browser from server?

This is the code i have in the action result that search for the image:

public FileResult SearchImage()
{

    var path = @"\\jesus-pc\Frontera\IMAGENES\SINGNOS DISTINTIVOS\0\80HP23891268272.TIF";
    var img1 = System.IO.File.ReadAllBytes(path);

    return File(im1, "image/jpg");
}

The image exists but this is what is displayed:

I tried with Convert.ToBase64String(img1) but I got this error:

the server responded with a status of 414 (Request-Uri Too Long)

What am I doing wrong?

Update:

My bad that I did not write the whole code of what I'm doing.

The last part that did not mentioned was that im using ajax to call the image.

When the user sends a request for information, there will be a link that will open a modal box. Inside that modal box is where the image must be displayed but the image will be loaded using ajax because the page should not be reloaded. So the image will be requested with ajax. Here is my code:

$(".doc").dblclick(function () {


$.ajax({
    url: "mainpage/SearchImage",
    data: "",
    type: "post",
    success: function (data) {
        ModalWindow.open();
        document.getElementById("img_1").src = data;
    }

})

解决方案

Consider using FileStreamResult instead of FileResult. This should solve your issue. The code shou look something like:

public FileStreamResult SearchImage()
{

    var path = 
       @"\\jesus-pc\Frontera\IMAGENES\SINGNOS DISTINTIVOS\0\80HP23891268272.TIF";
    var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
    return new FileStreamResult(fileStream, "image/jpeg");
}

Another thing that takes my attention is mime-type specified. In filename it's tif but in code it's jpeg.

Update

Why are you using ajax for this? You can do something like this instead:

$('#img_1').attr('src', 'mainpage/SearchImage');

这篇关于如何连接code图像在浏览器中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆