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

CKFinder how to get dimension URL and Dimension (width/height) when choosing an image (files:choose)?

本文关键字:选择 宽度 高度 文件 获取 CKFinder URL 图像      更新时间:2023-09-26

我正在使用CkFinder 3,成功上传后,我需要在用户单击"选择"按钮后能够检测到的图像:

  • 文件名/标识
  • 网址
  • 原始图像的宽度和高度

目前我正在使用files:choose但我无法在 cb 事件中找到此信息。

知道如何解决吗?代码示例将不胜感激,谢谢。

    CKFinder.modal( {
        connectorPath: 'https://api.mysite.com/lib/ckfinder/core/connector/php/connector.php',
        resizeImages: false, 
        startupFolderExpanded: true,
        chooseFiles: true,
        width: 1000,
        height: 800,
        language: 'en',
        onInit: function( finder ) {
            finder.on( 'files:choose', function( evt ) {
            } );
        }
    } );

files:choose事件描述中获取示例。您将获得用户在 evt.data.files 中选择的文件Backbone.Collection

棘手的部分是从服务器ImageInfo获取图像尺寸(使用 command:send 请求(,因为它需要处理带有 promise 的异步代码。

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

// 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。