本文介绍了如何使用Ajax和preVIEW它来上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图上传图片到图片文件夹使用AJAX,但它不工作当我选择一个图像,然后将图像应上传到我的本地硬盘上的文件夹,并进一步我会利用这一点。

Am trying to upload an image to images folder using ajax but its not working when i choose an image then the image should upload to my folder in local hard drive and further i will use that.

请帮我

我的code是:

SCRIPT     

$(document).ready(function() { 
$('#pathAbout,#path,#pathWork').change(function(){
if($(this).val() !=""){
    $.ajax({
       type: "POST",
       url: "imageLoad.php", 
       data: "name = <?php echo $_FILES['imgPath']['name']; ?>&type=<?php echo $_FILES['imgPath']['type']; ?>&tmp_name=<?php echo $_FILES['imgPath']['tmp_name']; ?>",
       success: function(msg){
        alert(msg);
       }
     });
}
});
}); 
</script>

HTML

<form method="POST" action="tabs.php?page=HOME" enctype="multipart/form-data">
<table>
<tr>
<td><label>Choose Image</label></td>
<td><input type="file" id ="pathAbout" name="imgPath" /></td>
</tr>

</table>
</form>

PHP

if(!empty($_POST)) {
if((isset($_POST['name']) && $_POST['name'] != '') || (isset($_POST['type']) && $_POST['type'] != '') ||(isset($_POST['tmp_name']) && $_POST['tmp_name'] != ''))
{
$name = $_POST['name'];
$type = $_POST['type'];
$tmp_name = $_POST['tmp_name'];
$extension = strtolower(substr($name, strpos($name , '.') +1));
if(($extension == 'png' || $extension == 'jpeg' || $extension == "jpg")&&($type == "image/png" || $type == "image/jpeg" || $type == "image/jpg"))
{
    $location = 'images/';
    if(move_uploaded_file($tmp_name,$location."$name"))
        {
            echo "Loaded";
        } else {
            echo "Error occured";
        }
} else {
    echo "Unsupported file format";
}
} else {
    echo "Please choose a file";
}
} else {
echo "No Information found";
}

这是我的code,但什么也没有发生,当我选择一个文件

This is my code but nothing is happening when i choose a file

推荐答案

如果你愿意,你可以使用 Uploadify 在这里,你还可以看到演示,并进行样品$ C $下,例如 -

If you want you can use Uploadify here you can also see demo and for sample code for eg-

$(function() {
$("#file_upload").uploadify({
    'formData'      : {'someKey' : 'someValue', 'someOtherKey' : 1},
    'swf'           : '/uploadify/uploadify.swf',
    'uploader'      : '/uploadify/uploadify.php',
    'onUploadStart' : function(file) {
        $("#file_upload").uploadify("settings", "someOtherKey", 2);
    }
});
});

$("#image_upload2").uploadify(uploadifyBasicSettingsObj);

uploadifyBasicSettingsObj.onUploadSuccess = function(file, data, response) 
{
    $('.tempImageContainer2').find('.uploadify-overlay').show();

    /* Here you actually show your uploaded image.In my case im showing in Div  */
    $('.tempImageContainer2').attr('style','background- image:url("../resources/temp/thumbnail/'+data+'")');
    $('#hidden_img_value2').attr('value',data);
}

在HTML code:

The HTML code:

<div class="boxWrapper tempImageContainer2" data-image-container-id="2">
   <input type="file"  accept="gif|jpg" name="image2" style="" id="image_upload2"/>
   <input type="hidden" value="" name="image[]" id="hidden_img_value2">
   <input type="hidden" name="upload_path" value="" id="upload_path2" class="upload_path">
   <div class="uploadify-overlay">Change</div>
   <a class="remove-pic-link" href="javascript:void(0)" style="display:none">Remove</a>
 </div>

这篇关于如何使用Ajax和preVIEW它来上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!