php - jQuery/ajax 上传图片并保存到文件夹

标签 php jquery ajax file-upload

更新下面的代码

我找到了一些能够上传图像并显示其缩略图的代码。但是,我也想将图像保存到特定文件夹中。

我可以使用什么 jQuery 代码或 ajax 代码将原始图像保存到我选择的文件夹中?

这是现场演示:http://jsfiddle.net/dn9Sr/2/

完整代码如下:

<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.3.js" type="text/javascript"></script>

<style>
.input-file-row-1:after {
    content: ".";
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0;
    height: 0;
}

.input-file-row-1{
    display: inline-block;
    margin-top: 25px;
    position: relative;
}

#preview_image {
  display: none;
  width: 90px;
  height: 90px;
  margin: 2px 0px 0px 5px;
  border-radius: 10px;
}

.upload-file-container { 
    position: relative; 
    width: 100px; 
    height: 137px; 
    overflow: hidden;   
    background: url(http://i.imgur.com/AeUEdJb.png) top center no-repeat;
    float: left;
    margin-left: 23px;
} 

.upload-file-container-text{
    font-family: Arial, sans-serif;
    font-size: 12px;
    color: #719d2b;
    line-height: 17px;
    text-align: center;
    display: block;
    position: absolute; 
    left: 0; 
    bottom: 0; 
    width: 100px; 
    height: 35px;
}

.upload-file-container-text > span{
    border-bottom: 1px solid #719d2b;
    cursor: pointer;
}

.one_opacity_0 {
  opacity: 0;
  height: 0;
  width: 1px;
  float: left;
}
</style>
<script>
$( document ).ready(function() {

   function readURL(input, target) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            var image_target = $(target);
            reader.onload = function (e) {
                image_target.attr('src', e.target.result).show();
            };
            reader.readAsDataURL(input.files[0]);
         }
     }

    $("#patient_pic").live("change",function(){
        readURL(this, "#preview_image")
    });

});
</script>

</head>

<body>
<form name="" method="post" action="#" class="feedback-form-1">
    <fieldset>
        <div class="input-file-row-1">
            <div class="upload-file-container">
                <img id="preview_image" src="#" alt="" />
                <div class="upload-file-container-text">
                    <div class = 'one_opacity_0'>
                        <input type="file" id="patient_pic" label = "add" />
                    </div>
                    <span> Add Photo </span>
                </div>
            </div>
        </div>
    </fieldset>
</form>
</body>

</html>

更新:我认为我走在正确的轨道上。我很接近,但我不知道从 jQuery 发送什么数据。我添加了一个 php scrit,它收到了成功的回电,但我没有发送正确的 var。我想如果我发送正确的 val 就可以得到它。 代码:

<script>
$( document ).ready(function() {

   function readURL(input, target) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            var image_target = $(target);
            reader.onload = function (e) {
                image_target.attr('src', e.target.result).show();


                 $.ajax({
            type:'POST',
            url: 'theUpload.php',
            data: input.files[0],
            success:function(data){
                console.log("success");
                console.log(data);
                alert(data);
            },
            error: function(data){
                console.log("error");
                console.log(data);
            }
        });



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








         }
     }

    $("#patient_pic").live("change",function(){
        readURL(this, "#preview_image")
    });

});
</script>

最佳答案

试试这个。

脚本:

function uploadFile(){
  var input = document.getElementById("file");
  file = input.files[0];
  if(file != undefined){
    formData= new FormData();
    if(!!file.type.match(/image.*/)){
      formData.append("image", file);
      $.ajax({
        url: "upload.php",
        type: "POST",
        data: formData,
        processData: false,
        contentType: false,
        success: function(data){
            alert('success');
        }
      });
    }else{
      alert('Not a valid image!');
    }
  }else{
    alert('Input something!');
  }
}

HTML:

<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>    
</head>
<body>
    <input type="file" id="file" />
    <button onclick="uploadFile();">Upload</button>
</body>
</html>

上传.php:

<?php
$dir = "image/";
move_uploaded_file($_FILES["image"]["tmp_name"], $dir. $_FILES["image"]["name"]);
?>

关于php - jQuery/ajax 上传图片并保存到文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21398731/

相关文章:

ajax - 使用 Ajax 从 Servlet 下载文件

php - 如何在同一代码位置创建数据库?

javascript - 通过 javascript 将文本(双引号存储在变量中)显示到模态时遇到问题

javascript - 编写一个简单的插件/组件

javascript - 如何从 Bootstrap Modal 发布文件和表单数据

android 模拟器 1.5、1.6、2.1 无法解析具有 <?xml 版本 ="1.0"编码 ="Shift-JIS"?> 的 XML 文件

php - preg_match 匹配扩展名为 .jpg/.png 的文件

php - csv 到 excel 转换

php - Braintree dropin UI : validate billing address custom fields before form submit

jquery - Javascript,从每个循环中获取唯一值或按 value.id 进行分组