admin管理员组

文章数量:1384246

I am trying to create a simple jquery file upload following the steps in this tutorial: / and it is fully functional and excellent. However I want to have more control and more security over what users can upload images, for example I want to be able to send a token as well. Here is my original code:

 $('#images').change(function (evt) {
    $('#response').text("Uploading . . .");
    var amount = this.files.length;
    var reader, file;
    alert(formdata);
    for (var i = 0; i < amount; i++ ) {
        file = this.files[i];
        if (!!file.type.match(/image.*/)) {
            if ( window.FileReader ) {
                reader = new FileReader();
                reader.onloadend = function (e) { 
                    appendUploadedPic(e.target.result, file.fileName);
                };
                reader.readAsDataURL(file);
            }
            if (formdata) {
                formdata.append("images[]", file);
            }
        }   
    }

    if (formdata) {
        $.ajax({
            url: "php/upload.php",
            type: "POST",
            data: formdata,
            dataType:'json',
            processData: false,
            contentType: false,
            success: function (res) {
                $('#response').html(res['image_name']); 
            }
        });
    }
});

and the php is simple so far

foreach ($_FILES["images"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $name = $_FILES["images"]["name"][$key];
        move_uploaded_file( $_FILES["images"]["tmp_name"][$key], "../members/" . $_FILES['images']['name'][$key]);
    }
}

$respond = array("filename" => $_FILES["images"]["name"][0], "type" => "image");
echo json_encode($respond);

now what I want to do is this

 $.ajax({
            url: "php/upload.php",
            type: "POST",
            data: 'token=someToken&code=upload&data='+formdata,
            dataType:'json',
            processData: false,
            contentType: false,
            success: function (res) {
                $('#response').html(res); 
            }
        });

Is it possible? If so, how would I read it from php? It its a post method i normally use a $_POST['code'] and $_POST'token'], but how would I read the images formdata object?

I am trying to create a simple jquery file upload following the steps in this tutorial: http://net.tutsplus./tutorials/javascript-ajax/uploading-files-with-ajax/ and it is fully functional and excellent. However I want to have more control and more security over what users can upload images, for example I want to be able to send a token as well. Here is my original code:

 $('#images').change(function (evt) {
    $('#response').text("Uploading . . .");
    var amount = this.files.length;
    var reader, file;
    alert(formdata);
    for (var i = 0; i < amount; i++ ) {
        file = this.files[i];
        if (!!file.type.match(/image.*/)) {
            if ( window.FileReader ) {
                reader = new FileReader();
                reader.onloadend = function (e) { 
                    appendUploadedPic(e.target.result, file.fileName);
                };
                reader.readAsDataURL(file);
            }
            if (formdata) {
                formdata.append("images[]", file);
            }
        }   
    }

    if (formdata) {
        $.ajax({
            url: "php/upload.php",
            type: "POST",
            data: formdata,
            dataType:'json',
            processData: false,
            contentType: false,
            success: function (res) {
                $('#response').html(res['image_name']); 
            }
        });
    }
});

and the php is simple so far

foreach ($_FILES["images"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $name = $_FILES["images"]["name"][$key];
        move_uploaded_file( $_FILES["images"]["tmp_name"][$key], "../members/" . $_FILES['images']['name'][$key]);
    }
}

$respond = array("filename" => $_FILES["images"]["name"][0], "type" => "image");
echo json_encode($respond);

now what I want to do is this

 $.ajax({
            url: "php/upload.php",
            type: "POST",
            data: 'token=someToken&code=upload&data='+formdata,
            dataType:'json',
            processData: false,
            contentType: false,
            success: function (res) {
                $('#response').html(res); 
            }
        });

Is it possible? If so, how would I read it from php? It its a post method i normally use a $_POST['code'] and $_POST'token'], but how would I read the images formdata object?

Share Improve this question asked Apr 12, 2013 at 22:08 lomas09lomas09 1,1044 gold badges13 silver badges29 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

If formdata was created with FormData, you append additional fields like this:

formdata.append(name, value);

So, if you want to add a token field and a code field, it's:

if (formdata) {
    formdata.append("images[]", file);
    formdata.append("token", "someToken");
    formdata.append("code", "someCode");
}

However, you should be aware that this method of uploading won't be supported by a lot of browsers, namely IE6-IE9, which represents about half of all internet users, I believe.

I remend using a plugin that detects browser support and uses the best available method. I wrote this plugin for exactly that purpose:

https://github./LPology/Simple-Ajax-Uploader

There's a link to a demo at the top to try it out. If you decide to use it and run into any issues, just ping me and I can help you out. Good luck.

本文标签: phpajax Sending File formdata along with variablesStack Overflow