通过使用 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]);
}
这种方法存在一些问题。
The Img's source will not be stored retained on Postback, you can create a workaround for this but it's a little indepth.
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/