dpt62283 2013-04-12 22:08
浏览 62
已采纳

$ .ajax发送文件formdata和变量

I am trying to create a simple jquery file upload following the steps in this tutorial: http://net.tutsplus.com/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?

  • 写回答

1条回答 默认 最新

  • dsarttv037029 2013-04-12 22:47
    关注

    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 recommend using a plugin that detects browser support and uses the best available method. I wrote this plugin for exactly that purpose:

    https://github.com/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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化