检查宽度和宽度使用javascript上传之前的图像高度?

时间:2014-07-18 04:05:33

标签: c# javascript jquery asp.net

我在Grid-view中使用FILE Upload-er,然后我动态地找到fileuploader的ID。但问题是iam得到高度和高度值0宽度。 在我使用的代码下面。

function validateFileSize() {
        debugger;
        for (var i = 1; i < document.getElementById('<%=gvDocuments.ClientID %>').rows.length; i++) {
            var uploadControl = document.getElementById('<%=gvDocuments.ClientID %>').rows[i].cells[3].getElementsByTagName('INPUT')[0].id; //** here i find the ID of File Uploader
            if (document.getElementById('<%=gvDocuments.ClientID %>').rows[i].cells[1].getElementsByTagName('SELECT')[0].value == "18")//** here i find the ID of DropDownlist
             {
                var newImg = new Image();
                newImg.src = document.getElementById(uploadControl).value;
                var height = newImg.height;
                var width = newImg.width;

                alert('The Image Dimension is ' + height + ' X ' + width + '');
            }
        }
    }

3 个答案:

答案 0 :(得分:2)

如果您使用的是jQuery,并且要求像这样的图像尺寸

检查此示例

http://jsbin.com/oTAtIpA/3/edit

答案 1 :(得分:0)

您只需要检查图像上载功能内的图像尺寸。

e.g

newImg.onload = function()
{
    var height = this.height;
    var width = this.width;
    // do stuff with image dimensions
}

另见https://stackoverflow.com/a/626505/53241

LittleDragon的例子也使用纯JavaScript来获取维度,而不是jQuery

答案 2 :(得分:0)

func loop(inputArray: [inputType], doneLoop: @escaping (_ firstArray: [firstType],_ secondArray: [secondType]) -> Void) {

    var firstArray: [firstType]?
    var nameArray: [secondType]?

    let dispatchGroup = DispatchGroup()

    for element in inputArray {

        let firstRef = Database.database().reference(withPath: "firstPath")

            //Enter dispatch group
            dispatchGroup.enter()
            firstRef.observeSingleEvent(of: .value, with: { snapshot in

                //Storing first snapshot value

                let firstSnapshotValue = snapshot.value as! firstType

                let secondRef = Database.database().reference(withPath: "secondPath")

                secondRef.observeSingleEvent(of: .value, with: { snapshot in


                    //Storing second snapshot value
                    let secondSnapshotValue = snapshot.value as? String

                    //Append retrieved values to arrays
                    firstArray?.append(firstSnapshotValue)
                    secondArray?.append(secondSnapshotValue)
                })

            })
            //Leave dispatch group
            dispatchGroup.leave()

    }

    //Notify on main queue and execute completion handler
    dispatchGroup.notify(queue: .main) {
        doneLoop(firstArray!, secondArray!)
    }


}

请尝试此