Javascript 在asp.net中使用javascript设置图片控件的图片URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11899485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:35:59  来源:igfitidea点击:

Use javascript to set image URL of image control in asp.net

javascriptimageimageurl

提问by Ishan

I have a Image, FileUpload and a Button controls. I want to save the image to the server from the local path obtained from FileUpload control. I implemented this functionality on Button Click in C#..

我有一个 Image、FileUpload 和一个 Button 控件。我想将图像从 FileUpload 控件获取的本地路径保存到服务器。我在 C# 中的 Button Click 上实现了这个功能..

Now i want to set the image URL of Image control OnClientClick of the same button on which server side code is implemented.

现在我想设置实现服务器端代码的同一按钮的图像控件 OnClientClick 的图像 URL。

Image URL will defer everytime depending on file selected in FileUpload control. Can anyone help me to understand how javascript can be used to set image URL based on thre file selected in File Upload Control?

图像 URL 将根据 FileUpload 控件中选择的文件每次延迟。任何人都可以帮助我了解如何使用 javascript 根据文件上传控件中选择的 thre 文件设置图像 URL?

回答by Ayman Safadi

First of all, understand that JavaScript doesn't understand, care, or even know about C# and its fancy "controls". It just deals with HTML. Period. That said, you can use JavaScript's setAttributefunction to set the image URL of an imgtag (not control). Like this:

首先,要了解 JavaScript 不了解、不关心,甚至不了解 C# 及其花哨的“控件”。它只处理 HTML。时期。也就是说,您可以使用 JavaScript 的setAttribute函数来设置img标签(而非控件)的图像 URL 。像这样:

document.getElementById('my-image').setAttribute('src', 'http://ecx.images-amazon.com/images/I/41%2BjAZ4dUGL._SS500_.jpg');

Demo here: http://jsfiddle.net/je9Gx/

演示在这里:http: //jsfiddle.net/je9Gx/

回答by Pranav

You can use this code to find the image control, where imgidis ID of image control;

您可以使用此代码找到图像控件,其中imgid图像控件的ID;

$("[id$='imgid']").attr("src",pathfromfileuploader);

//pathfromfileuploader=it is a variable which stores the path taken from file uploader;

Hope it will help :)

希望它会有所帮助:)