Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

javascript - 上载图像之前预览图像(Preview an image before it is uploaded)

I want to be able to preview a file (image) before it is uploaded.

(我希望能够在上传文件之前预览文件(图像)。)

The preview action should be executed all in the browser without using Ajax to upload the image.

(预览操作应在浏览器中全部执行,而无需使用Ajax上载图像。)

How can I do this?

(我怎样才能做到这一点?)

  ask by Simbian translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please take a look at the sample code below:

(请看下面的示例代码:)

 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); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form runat="server"> <input type='file' id="imgInp" /> <img id="blah" src="#" alt="your image" /> </form> 

Also, you can try this sample here .

(另外,您可以在此处尝试该示例 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...