如何使用文件上传和图像按钮查看上传的图像c# [英] how to view uploaded image using file upload and imagebutton c#

查看:84
本文介绍了如何使用文件上传和图像按钮查看上传的图像c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多代码但无法在图像按钮中查看上传的文件

i have tried a lot of code but unable to view the uploaded file in image button

推荐答案

尝试按以下方式执行此操作。



1 - 根据您所需的层次结构将图像存储在应用程序的本地文件夹中。

2 - 将图像名称存储在数据库中。



要查看上传图片,请执行以下操作。



从数据库中获取图像名称并使用如下所示来显示图像。



Try to do this in the following way.

1-Store image in application's local folder as per your desired hierarchy.
2-Store image name in the db.

TO VIEW the upload images, do the following.

Get the images name from the db and use like below to show the images.

<asp:Image ID="imgUploaded" Height="100px" Width="100px" runat="server" data-lightbox="img1" ImageUrl='<%#"~/Images/"+Eval("ImageName") %>' />





解释:图像URL由来自数据源的半硬编码路径和图像名称组成(查询返回的数据,即EVAL())。





希望它会对你有所帮助。对于任何进一步的查询,评论或反对,大多数人欢迎。 :)



EXPLAINATION: Image URL consist of half hard coded path and image name from the datasource(query returned data i.e in EVAL()).


Hope it will help you. For any further query, comment or objection, Most welcomed. :)


嗨...

Hi...
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function previewFile() {
            var preview = document.querySelector('#<%=Avatar.ClientID %>');
            var file = document.querySelector('#<%=avatarUpload.ClientID %>').files[0];
            var reader = new FileReader();

            reader.onloadend = function () {
                preview.src = reader.result;
            }

            if (file) {
                reader.readAsDataURL(file);
            } else {
                preview.src = "";
            }
        }
    </script>
</head> 




<input ID="avatarUpload" type="file" name="file"  önchange="previewFile()"   runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
<asp:Image ID="Avatar" runat="server" Height="225px" ImageUrl="~/Images/NoUser.jpg" Width="225px" />   




protected void Upload(object sender, EventArgs e)
{
    int contentLength = avatarUpload.PostedFile.ContentLength;//You may need it for validation
    string contentType = avatarUpload.PostedFile.ContentType;//You may need it for validation
    string fileName = avatarUpload.PostedFile.FileName;
    avatarUpload.PostedFile.SaveAs(@"c:\test.tmp");//Or code to save in the DataBase.
}   



谢谢你。


Thank u.


这篇关于如何使用文件上传和图像按钮查看上传的图像c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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