简体   繁体   English

如何在 asp.net 中使用 javascript 显示和上传图像?

[英]How to show and upload images with javascript in asp.net?

I don't known how to show images and upload with Javascript, don't use ( input type file of html , control fileupload asp.net ) .我不知道如何显示图像并使用 Javascript 上传,请勿使用(输入类型文件为 html ,控制文件上传 asp.net )。 I want to use as follow我想使用如下

#img{width:100px;height:100px;}
<div id="img"></div>
<button type="button" id="bt_image">Image</button>
<button type="button" id="bt_save">Save</button>

When I click button Image , show dialog choose file, choose file image and the image will show on div img, click button Save , this upload in server.当我单击按钮 Image 时,显示对话框选择文件,选择文件图像,图像将显示在 div img 上,单击按钮 Save ,此上传到服务器。 Thank you.谢谢你。

Try this example - there's an upload button, and a preview area, all you need to add is the upload button.试试这个例子- 有一个上传按钮和一个预览区域,您需要添加的只是上传按钮。

<form id="form1" runat="server">
    <input type='file' id="imgInp" />
    <img id="blah" src="#" alt="your image" />
</form>

And the js:和js:

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

        reader.onload = function (e) {
            $('#blah').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

$("#imgInp").change(function(){
    readURL(this);
});

Javascript version Javascript 版本

function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();    
        reader.onload = function (e) {document.querySelector('#blah').attr('src', e.target.result);}  
        reader.readAsDataURL(input.files[0]);
    }
}   
document.querySelector("#imgInp").change(function(){readURL(this);});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM