上传图像时jquery-mobile上的image.onError

时间:2016-09-02 07:26:27

标签: javascript jquery-mobile

我正在使用PhoneGap来运行我的JQuery Mobile Hybrid App。

我正在关注此tutorial以便将我的设备(Samsung Note4,Android 6.0.1)上的图像上传到canvas,我尝试过不同类型的图片,但我总是得到image.onError的执行结果。

获得image.onError执行的原因是什么?

以下是教程中的代码:

function processFile(dataURL, fileType) {
    var maxWidth = 800;
    var maxHeight = 800;

    var image = new Image();
    image.src = dataURL;

    image.onload = function () {
        var width = image.width;
        var height = image.height;
        var shouldResize = (width > maxWidth) || (height > maxHeight);

        if (!shouldResize) {
            sendFile(dataURL);
            return;
        }

        var newWidth;
        var newHeight;

        if (width > height) {
            newHeight = height * (maxWidth / width);
            newWidth = maxWidth;
        } else {
            newWidth = width * (maxHeight / height);
            newHeight = maxHeight;
        }

        var canvas = document.createElement('canvas');

        canvas.width = newWidth;
        canvas.height = newHeight;

        var context = canvas.getContext('2d');

        context.drawImage(this, 0, 0, newWidth, newHeight);

        dataURL = canvas.toDataURL(fileType);

        sendFile(dataURL);
    };

    image.onerror = function () {
        alert('There was an error processing your file!');
    };
}

0 个答案:

没有答案