【问题标题】:jQuery / ajax upload image and save to folderjQuery / ajax 上传图片并保存到文件夹
【发布时间】:2014-02-19 08:18:56
【问题描述】:

更新以下代码

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

我可以使用哪些 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>

【问题讨论】:

    标签: php jquery ajax file-upload


    【解决方案1】:

    试试这个。

    脚本:

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

    【讨论】:

      【解决方案2】:

      John 的回答很好,但 file = input.files[0] 对我不起作用

      file = input[0].files[0]
      

      有效。

      【讨论】:

        【解决方案3】:

        您需要一种服务器端语言来将上传的图像存储到指定文件夹。 PHP 就是其中之一,所以我强烈建议您看看它。

        AJAX 仅用于在不刷新页面的情况下执行服务器端调用。

        【讨论】:

        • 好的,我也能用php了。
        • 我确定有一些 ajax 代码可以上传到 php 脚本,其中包含正确的信息?
        • 那是正确的.. 通过 AJAX 调用,您指的是您制作的 PHP 脚本。最好的选择是将 jQuery 与 AJAX 调用一起使用。您可以在jquery.com查看更多信息
        • 我想我已经接近了:data: input.files[0],是我遇到问题的那一行。 ajax 似乎在工作,但我没有发送正确的 var。
        猜你喜欢
        • 2017-02-25
        • 2020-05-20
        • 2018-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 2013-02-14
        • 1970-01-01
        相关资源
        最近更新 更多