且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

CKFinder如何在选择图像时获取尺寸URL和尺寸(宽度/高度)(文件:选择)?

更新时间:2023-02-23 19:21:50

files:选择 事件描述。你得到 Backbone.Collection 用户在 evt.data.files 中选择的.cksource.com / ckfinder3 /#!/ api / CKFinder.Models.File>文件。

Taking example from the files:choose event description. You get Backbone.Collection of files chosen by the user in evt.data.files.

棘手的部分是从 ImageInfo 服务器(使用 命令:发送 请求)因为它需要使用promises处理异步代码。

The tricky part is to obtain image dimensions from ImageInfo server (using the command:send request) since it requires to process asynchronous code with promises.

假设您只允许上传图片,示例代码如下:

Assuming that you only allow Images to be uploaded, example code is below:

// Listen to event.
finder.on( 'files:choose', function( evt ) {
    // Iterate over the files collection.
    evt.data.files.forEach( function( file ) {
        // Send command to the server.
        finder.request( 'command:send', {
            name: 'ImageInfo',
            folder: file.get( 'folder' ),
            params: { fileName: file.get( 'name' ) }
        } ).done( function( response ) {
            // Process server response.
            if ( response.error ) {
                // Some error handling.
                return;
            }

            // Log image data:
            console.log( '-------------------' );
            console.log( 'Name:', file.get( 'name' ) );
            console.log( 'URL:', file.getUrl() );
            console.log( 'Dimensions:', response.width + 'x' + response.height );
            console.log( 'Size:', response.size + 'B' );
        } );
    } );
} )

如果您使用任何远程后端,例如Dropbox,您可能需要使用 file:getUrl 请求获取文件的URL。

If you are using any remote backend such us Dropbox you might need to use the file:getUrl request to obtain the file's URL.