首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ajax通过表单上传图片

Ajax通过表单上传图片
EN

Stack Overflow用户
提问于 2016-02-21 14:46:41
回答 1查看 35关注 0票数 0

我有一个表单中的数据,这是在弹出窗口中打开。其可由用户编辑。除图像外,所有数据都已编辑。它给我一个错误"undefined index file in c/xamp/htdocs/website/file.php on line 35“

代码语言:javascript
运行
复制
my ajax call is :
function updat(id){
$.ajax({
        url:'file.php?upd='+id,
        success:function(response){

var img = $("#file").val();
var marlas = $("#marlas").val();
var bath= $("#bath").val();
var bed = $("#bed").val();
var house_no = $("#house_no").val();
var address = $("#address").val();
var price = $("#price").val();
var ids = $("#ids").val();
var dataString = 'files='+ img + '&marlas1='+ marlas + '&bath1='+ bath + '&bed1=' + bed + '&house_no1=' + house_no + '&address1=' + address +  '&price1=' + price +'&ids1=' + ids;

$.ajax({
type: "POST",
url: "file.php",
data: dataString,
success: function(result){
document.getElementById("update");
alert(result);

window.location.reload();
}
});
}
});
}

my file code is :

if(isset($_POST['ids1'])){
$ids= $_POST['ids1'];
$file= $_POST['files'];
 $target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["files"]["name"]);
print_r($target_file); exit();
  $file=$_FILES["files"]["name"];
 $fsize=$_FILES["files"]["size"];
   $ftype=$_FILES["files"]["type"];
    $tmp_name=$_FILES["files"]["tmp_name"];
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
}
    move_uploaded_file($_FILES["files"]["tmp_name"], $target_file);
$marlas= $_POST['marlas1'];
$bath= $_POST['bath1'];
$bed= $_POST['bed1'];
$house_no= $_POST['house_no1'];
$address= $_POST['address1'];
$price= $_POST['price1'];
$q= mysql_query("update property  set img='$file', marlas='$marlas', bath='$bath', bed='$bed', house_no= '$house_no', address='$address', price='$price' where id='$ids'"); 
if(!$q){echo 'error'.mysql_error(); } 
    else echo "property updated";   
}
EN

回答 1

Stack Overflow用户

发布于 2016-02-21 15:02:03

您可以创建一个函数来处理文件上传并重用它。下面的代码说明了这一点。此外,该函数有一个回调,在上传成功时会调用该回调。

代码语言:javascript
运行
复制
 var Url = "YourApplicationUrlToPostTheFile"; //file.php
    AjaxFileUpload($('input[type="file"]')[0], Url, function (response) {
        alert('File Uploaded, And the response is ' + response);
       // do something after the image is uploaded successfully;
    });

以及上传文件的Ajax函数。

代码语言:javascript
运行
复制
 function AjaxFileUpload($element, Url, callback) {

    var formdata = new FormData();
    var totalFiles = $element.files.length;   

    if (totalFiles > 0) {
        for (var i = 0; i < totalFiles; i++) {
            var file = $element.files[i];

            formdata.append("FileUpload", file);
        }

        $.ajax({
            type: "POST",
            url: Url,               
            data: formdata,
            contentType: false,
            processData: false,
            success: function (result) {
                callback(result);
            },
            error: function (error) {
                console.log("File Upload Failure");
            }
        });       
    }    
}

如果你有任何问题,请告诉我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35533151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档