c# - 填充 asp :Image with an image selected within an asp:FileUpload control

标签 c# asp.net

通过使用 asp FileUpload 控件,只要我在 FileUpload 控件中选择一个图像,我是否就可以使用图像填充一个 asp 图像,而无需将其上传到文件夹等,只需使用图像填充 asp Image 控件已选择。

最佳答案

这里有一种直接在客户端执行此操作的方法:JSFiddle

var input = document.getElementById('fileUp');
var img = document.getElementById('img1');

if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function (e) {
        img.src =  e.target.result;
    }
    reader.readAsDataURL(input.files[0]);
}

这种方法存在一些问题。

  1. The Img's source will not be stored retained on Postback, you can create a workaround for this but it's a little indepth.

  2. The use of this requires support for the HTML5 Web FileReader API. Which is only supported by a limited amount of browsers (See full support list at the bottom of the link I posted)

如果您仅需要此功能用于客户端处理并且仅适用于 IE10/Chrome/其他支持的浏览器,那么您就可以了。 否则此方法将不适合您。

关于c# - 填充 asp :Image with an image selected within an asp:FileUpload control,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20423846/

相关文章:

c# - 如何确定有害队列消息的原因

c# - 生成集合的排列 - 高效且有区别

javascript - Asp.Net 控制值在 Jquery 中返回未定义

c# - ds.Tables[0].Select() 代表什么

asp.net - 元素 'ToolkitScriptManager' 不是已知元素

c# - 没有控制台窗口的 AppServiceBridge

c# - 如何在 IIS 应用程序池崩溃、启动或停止时收到通知

c# - System32 文件夹的 Environment.GetFolderPath(Environment.SpecialFolder.System) 和 Environment.SystemDirectory 之间的区别

c# - Request.Url.Host 与 Request.Url.Authority

c# - 在 Kentico 中提交 Authorize.Net SIM 表格