dongsi1954 2015-03-06 15:05
浏览 13

将$ FILE数据传递给$ .getJSON

I want to pass the $_FILES array to .getJSON.

Here is my 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);
                }
            });

Here is my 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';
}

Is there a solution for this, can I pass an image through .getJSON?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 AT89C51控制8位八段数码管显示时钟。
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题