jQuery AJAX 上传图片
JavaScript Code
- <script type="text/javascript">
- function showPreview(objFileInput) {
- if (objFileInput.files[0]) {
- var fileReader = new FileReader();
- fileReader.onload = function (e) {
- $("#targetLayer").html('<img src="'+e.target.result+'" width="200px" height="200px" class="upload-preview" />');
- $("#targetLayer").css('opacity','0.7');
- $(".icon-choose-image").css('opacity','0.5');
- }
- fileReader.readAsDataURL(objFileInput.files[0]);
- }
- }
- $(document).ready(function (e) {
- $("#uploadForm").on('submit',(function(e) {
- e.preventDefault();
- $.ajax({
- url: "upload.php",
- type: "POST",
- data: new FormData(this),
- beforeSend: function(){$("#body-overlay").show();},
- contentType: false,
- processData:false,
- success: function(data)
- {
- $("#targetLayer").html(data);
- $("#targetLayer").css('opacity','1');
- setInterval(function() {$("#body-overlay").hide(); },500);
- },
- error: function()
- {
- }
- });
- }));
- });
- </script>
XML/HTML Code
- <div id="body-overlay"><div><img src="loading.gif" width="64px" height="64px"/></div></div>
- <div class="bgColor">
- <form id="uploadForm" action="upload.php" method="post">
- <div id="targetOuter">
- <div id="targetLayer"></div>
- <img src="photo.png" class="icon-choose-image" />
- <div class="icon-choose-image" >
- <input name="userImage" id="userImage" type="file" class="inputFile" onChange="showPreview(this);" />
- </div>
- </div>
- <div>
- <input type="submit" value="Upload Photo" class="btnSubmit" />
- </form>
- </div>
- </div>
原文地址:http://www.freejs.net/article_biaodan_674.html