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

6

以下是更新的代码

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

有哪些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脚本,并且它正在成功地进行回调,但我没有发送正确的变量。我认为如果我只发送正确的值,我就可以得到它。

<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>
3个回答

18

试试这个。

脚本:

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"]);
?>

3

John的答案很好,但file = input.files [0]对我没有效果。

file = input[0].files[0]

工作正常。


1
你需要一种服务器端语言来将上传的图像存储到指定文件夹中。 PHP 是其中之一,我强烈建议您了解一下它。
AJAX 只是为了在不刷新页面的情况下执行服务器端调用。

好的,我也能使用 PHP。 - Papa De Beau
我相信有一些ajax代码可以上传包含信息的php脚本,对吧? - Papa De Beau
没错,你在那个AJAX调用中引用了你编写的PHP脚本。最好的选择是使用jQuery进行AJAX调用。你可以在http://www.jquery.com上查看更多信息。 - B_CooperA
我认为我已经接近了:data: input.files[0],这一行是我遇到问题的地方。Ajax似乎正在工作,但我没有发送正确的变量。 - Papa De Beau

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接