如何获取由jquery在客户端设置图像的asp图像控件的ImageUrl? [英] how to Get ImageUrl of a asp image control whose image is set by jquery in the client side?

查看:70
本文介绍了如何获取由jquery在客户端设置图像的asp图像控件的ImageUrl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在aspx页面中有一个图像和按钮服务器控件.

i have an image and button server controls in the aspx page.

<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />  
<asp:Image ID="impPrev" runat="server" Height="200px" /> 
<input type="file" name="ImageUpload" id="ImageUpload" onchange="ShowPreview(this)" />

我也有一个html文件类型输入控件,通过它我可以使用jquery之类的方法从客户端更改asp图像控件的imageUrl

i also have a html file type input control using which i am able to change the imageUrl of the asp image control from client side using jquery like

function ShowPreview(FileUpload1) {
   if (FileUpload1.files && FileUpload1.files[0]) {
      var file = FileUpload1.files[0];
      var ImageDir = new FileReader();
      if (file) {
         ImageDir.readAsDataURL(file);
      }  
      ImageDir.addEventListener('load', function () {
         $('#impPrev').attr('src', ImageDir.result);
      });
   }
}

如果我试图从像

protected void btnUpload_Click(object sender, EventArgs e) {
   var path = impPrev.ImageUrl;
}

空字符串返回到路径". 我如何获取jquery设置的图像的实际路径?

empty string is being returned to "path". how can i get the actual path of the image set by jquery??

推荐答案

我相信您无法将值获取到服务器端,因为该表单不会回发信息以到达服务器.此外,该图像未存储在服务器中的任何位置以从服务器端获取URL路径.但是,您可以通过使用隐藏字段并将该字段设置为所需的值来获取名称",客户路径"等图像"属性.

I believe you cannot get the value to server side because the form is not post back for the information to reach server. Moreover the image is not stored any where in the server to get the URL path from server side. But you can get the Image attributes such as Name, client path etc by using hidden field and setting the required value to that field.

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />  
        <asp:Image ID="impPrev" runat="server" Height="200px" /> 
        <input type="file" name="ImageUpload"   id="ImageUpload" onchange="ShowPreview(this)" />
       ** <input type="hidden" runat="server" id="myhid" />**
    </div>
    </form>
</body>
</html>

function ShowPreview(FileUpload1) {
    if (FileUpload1.files && FileUpload1.files[0]) {
        var file = FileUpload1.files[0];
        var ImageDir = new FileReader();
        if (file) {
            ImageDir.readAsDataURL(file);
        }
        ImageDir.addEventListener('load', function () {
        $('#impPrev').attr('src', ImageDir.result);
        $('#myhid').val(file.name);
        }); 
    }
}

 protected void btnUpload_Click(object sender, EventArgs e)
        {
            //var path = impPrev.ImageUrl;
            var test = myhid.Value;
        }

这篇关于如何获取由jquery在客户端设置图像的asp图像控件的ImageUrl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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