将$ FILE数据传递给$ .getJSON

时间:2015-03-06 15:05:41

标签: php file-upload upload image-uploading getjson

我想将$ _FILES数组传递给.getJSON。

这是我的jquery:

$.getJSON(link+'api/images/upload.php',{action:'image.upload',id:id,img:img}, function (response){
                var url = this.url;
                var result = response.message;
                if(response.status == 200){
                    var data = response.data;

                    console.log(data);
                    try_print('Data',data,try_dbg);
                }
                else{
                    alert(response.message);
                }
            });

这是我的api:

switch($_REQUEST['action']){
case 'image.upload':

    $id         = $_REQUEST['id'];
    $img        = $_FILES['img']['name']; //echo $img;
    $temp       = explode(".", $_FILES["img"]["name"]);
    $extension  = end($temp);
    $error      = $_FILES["img"]["error"];

    if(!empty($id)){
        if(!empty($img)){
            if(is_writable($original)){
                if(in_array($extension, $allowedExts)){
                    if($error == 0){

                        $filename = $_FILES["img"]["tmp_name"];
                        list($width, $height) = getimagesize($filename);

                        $a = apiPicture($original, $id, $_FILES["img"]);

                        $return['status']   = 'success';
                        $return['url']      = $original.$_FILES["img"]["name"];
                        $return['width']    = $width;
                        $return['height']   = $height;
                        $return['status']   = 200;
                        $return['message']  = 'Success.';
                    }
                    else{
                        $return['status']   = 400;
                        $return['message']  = 'Error: '.$error;
                    }
        .... the rest of the error codes
    break;
default:
    $return['message']  =   'Unknown Request';
}

是否有解决方案,我可以通过.getJSON传递图像吗?

0 个答案:

没有答案