如何将图像控件直接绑定到图像而不保存?

时间:2013-11-17 03:36:26

标签: c# asp.net file-upload imageurl

按钮点击事件我已按此编码直接从文件控件查看图像。

protected void btnupload_Click(object sender, EventArgs e)
    {
        try
        {


            if (photoupload.HasFile)
            {

                imageuploaded.ImageUrl = "~/./" + string.Format( photoupload.PostedFile.FileName);
                if (imageuploaded.ImageUrl != null)
                    lblerror.Text = "File is Uploaded";
            }
            else
            {
                lblmsg.Text = "Please check all the fields";
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
}

并且从该编码获得的图像是

http://localhost:4269/IMAG0990.jpg

其中IMAG990.jpg是我的图像名称 但图像在图像控制中可见,可能是因为此网址不正确 如何直接在图像控件中查看照片而不将其保存到任何文件夹。

1 个答案:

答案 0 :(得分:1)

测试此代码,它可以在IE上显示没有保存的显示图像

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<script type="text/javascript">
    function setImage() {
        a1 = 'file://localhost/' + document.getElementById('file').value;
        a1 = a1.toLowerCase();
        if (a1.substring(0, a1.lastIndexOf('.png')) || a1.substring(0, a1.lastIndexOf('.jpg')) || a1.substring(0, a1.lastIndexOf('.jpeg')) || a1.substring(0, a1.lastIndexOf('.gif'))) {
            var img = document.createElement('img');
            img.setAttribute('src', a1);
            document.getElementById('prevImage').appendChild(img);
        } 
    }
</script>

</head>

<body>

<input type="file" id="file" />
<input type="button" value="preview" onclick="setImage();" />
<div id="prevImage"></div>

</body>

</html>