getUploadedFiles

2.2.1 버전부터 지원합니다.

업로드된 파일의 경로와 삭제된 상태를 반환하며, 선택적으로 파일 타입으로 필터링할 수 있습니다.

유용한 데이터를 반환하려면, afterUploadFile, afterUploadImage, 그리고 afterUploadVideo 이벤트 핸들러에서 업로드된 파일을 등록하기 위해 반드시 addUploadPath를 호출해야 합니다.

매개변수

이름타입설명
[fileType]string'image', 'video', 또는 'file'. 생략하면 모든 타입을 반환합니다.

반환값

fileType이 제공된 경우:

타입설명
Array[{ path: '/upload/path/filename.png', isDeleted: false }, ...]

fileType이 생략된 경우:

타입설명
Object{ image: [...], video: [...], file: [...] } (각 항목에는 path와 isDeleted가 있음)
  • path: 업로드 경로
  • isDeleted: 에디터에서 파일이 제거되었는지 여부

예시

var filesData  = editor.getUploadedFiles();          // all types
var filesData2 = editor.getUploadedFiles('image');   // images only

사용 예시

1. 이미지 업로드 개수 제한

파일 업로드 관련 API를 beforeUploadImage / afterUploadImage 이벤트와 함께 사용하여 업로드할 수 있는 이미지의 최대 개수를 제한합니다.

var editorId = 'synapEditor';
var editorConfig = {};
var html = '';
var editor = new SynapEditor(editorId, editorConfig, html);

editor.setEventListener('afterUploadImage', function (e) {
    var fileType = e.fileType;
    var uploadPath = e.path;
    // Required: register the upload so getUploadedFiles() can see it.
    e.editor.addUploadPath(fileType, uploadPath);
});

editor.setEventListener('beforeUploadImage', function (e) {
    var fileType = e.fileType;
    var uploadCount = e.uploadCount; // number of images currently uploading (not yet uploaded)
    var imageCount = 0;
    e.editor.getUploadedFiles(fileType).forEach(function (info) {
        if (!info.isDeleted) {
            imageCount++; // count images that are still present
        }
    });
    if (imageCount + uploadCount >= 5) { // allow only up to 5
        e.returnValue = false; // cancel the upload
        alert('Only up to 5 images can be uploaded'); // "You can upload up to 5 images only."
    }
});