douzuanze0486 2017-07-09 10:37
浏览 84
已采纳

pathInfo()在接收附加到FormData的blob时表现得很奇怪

I am a bit confused, is this an expected behaviour?

Javascript - Sample 01

    function convertCanvasToImage() {
                    var temp_ctx, temp_canvas;
                    temp_canvas = document.createElement('canvas');
                    temp_ctx = temp_canvas.getContext('2d');
                    temp_canvas.width = windowWidth;
                    temp_canvas.height = windowWidth;
                    temp_ctx.drawImage(ctx.canvas, cutoutWidth, cutoutWidth, windowWidth, windowWidth, 0, 0, windowWidth, windowWidth);

                    var multiPart = new FormData();
                    temp_canvas.toBlob(function (blob) {

                        //Tilføjer blob til form objektet
                        multiPart.append('pernille', blob, ".jpg");

                        //Ajax kaldet
                        var http = new XMLHttpRequest();
                        var url = "ajax.php";
                        http.open("POST", url, true);
                        http.onreadystatechange = function () {
                            if (http.readyState === 4 && http.status === 200) {
                                alert(this.responseText);
                                console.log(this.responseText);
                            }
                        };
                        http.send(multiPart);
                    }, "image/jpeg");
                }

Javascript - Sample 02

        function convertCanvasToImage() {
            var temp_ctx, temp_canvas;
            temp_canvas = document.createElement('canvas');
            temp_ctx = temp_canvas.getContext('2d');
            temp_canvas.width = windowWidth;
            temp_canvas.height = windowWidth;
            temp_ctx.drawImage(ctx.canvas, cutoutWidth, cutoutWidth, windowWidth, windowWidth, 0, 0, windowWidth, windowWidth);

            var multiPart = new FormData();
            temp_canvas.toBlob(function (blob) {

                //Tilføjer blob til form objektet
                multiPart.append('pernille', blob, "pernille");

                //Ajax kaldet
                var http = new XMLHttpRequest();
                var url = "ajax.php";
                http.open("POST", url, true);
                http.onreadystatechange = function () {
                    if (http.readyState === 4 && http.status === 200) {
                        alert(this.responseText);
                        console.log(this.responseText);
                    }
                };
                http.send(multiPart);
            }, "image/jpeg");
        }

Ajax

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $file = current($_FILES);
            $nameparts = pathinfo($file['name']);

            echo "<pre>";
            echo '$_POST';
            print_r($_POST);
            echo '$_FILES';
            print_r($file);
            echo 'pathinfo';
            print_r($nameparts);
            echo "</pre>";
    }

The difference of the two javascript functions are these two lines

Func 01

    multiPart.append('pernille', blob, ".jpeg");

Func 02

    multiPart.append('pernille', blob, "pernille");

Javascript sample 01 will output this from the AJAX call There is no filename?

    <pre>$_POSTArray
    (
    )
    $_FILESArray
    (
        [name] => .jpg
        [type] => image/jpeg
        [tmp_name] => /home/xch07.wi2/tmp/phpZ6zxv2
        [error] => 0
        [size] => 17713
    )
    pathinfoArray
    (
        [dirname] => .
        [basename] => .jpg
        [extension] => jpg
        [filename] => 
    )
    </pre>

Javascript sample 02 will output this from the AJAX call There is a filename but no extension?

<pre>$_POSTArray
(
)
$_FILESArray
(
    [name] => pernille
    [type] => image/jpeg
    [tmp_name] => /home/xch07.wi2/tmp/phpaKQwU4
    [error] => 0
    [size] => 17601
)
pathinfoArray
(
    [dirname] => .
    [basename] => pernille
    [filename] => pernille
)
</pre>
  • 写回答

1条回答 默认 最新

  • doumu4032 2017-07-09 10:49
    关注

    It is not acting weird, rather, you are using FormData.append() method incorrectly.

    Correct form should be :

    multiPart.append('pernille',   blob,   'pernille.jpeg');
                         ^           ^            ^
                     field-name  data-value   file-name
    

    You need to append the file extension with the filename itself.

    Refer here to learn more.

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看