programing

Dropzone.js를 사용하여 서버에 이미 저장된 파일을 어떻게 보여줄 수 있습니까?

nicescript 2021. 1. 14. 08:05
반응형

Dropzone.js를 사용하여 서버에 이미 저장된 파일을 어떻게 보여줄 수 있습니까?


이해가 안 call돼요 ... 항상 정의되지 않았어요

모의 파일을 만듭니다.

var mockFile = { name: "Filename", size: 12345 };

기본 addedfile 이벤트 핸들러 호출

myDropzone.options.addedfile.call(myDropzone, mockFile);

선택적으로 파일의 썸네일을 표시합니다.

myDropzone.options. thumbnail.call(myDropzone, mockFile, "/image/url");

드디어 !!

$(function() {
    var mockFile = { name: "banner2.jpg", size: 12345 };
    var myDropzone = new Dropzone("#my-awesome-dropzone");
    myDropzone.options.addedfile.call(myDropzone, mockFile);
    myDropzone.options.thumbnail.call(myDropzone, mockFile, "http://localhost/test/drop/uploads/banner2.jpg");
})

다음 코드로도 수행 할 수 있습니다.

  <script>
        Dropzone.options.myAwesomeDropzone = false;
        Dropzone.autoDiscover = false;
        $("#image").dropzone({
            url: "http://someserver.com/upload.php",
            paramName: "image", // The name that will be used to transfer the file
            maxFilesize: 2, // MB
            maxFiles: 5,
            parallelUploads: 5,
            addRemoveLinks: true,
            dictMaxFilesExceeded: "You can only upload upto 5 images",
            dictRemoveFile: "Delete",
            dictCancelUploadConfirmation: "Are you sure to cancel upload?",
            accept: function (file, done) {
                console.log(file)
                if ((file.type).toLowerCase() != "image/jpg" &&
                        (file.type).toLowerCase() != "image/gif" &&
                        (file.type).toLowerCase() != "image/jpeg" &&
                        (file.type).toLowerCase() != "image/png"
                        ) {
                    done("Invalid file");
                }
                else {
                    done();
                }
            },
            init: function () {
                var mockFile = { name: "myimage.jpg", size: 12345, type: 'image/jpeg' };
                this.addFile.call(this, mockFile);
                this.options.thumbnail.call(this, mockFile, "http://someserver.com/myimage.jpg");
            }
        });
    </script>

편집하다

Dropzone 4.0 init기능 업데이트는 다음 과 같이 호출 할 수 있습니다.

init: function () {
   var mockFile = { name: "myimage.jpg", size: 12345, type: 'image/jpeg' };       
   this.options.addedfile.call(this, mockFile);
   this.options.thumbnail.call(this, mockFile, "http://someserver.com/myimage.jpg");
   mockFile.previewElement.classList.add('dz-success');
   mockFile.previewElement.classList.add('dz-complete');
}

내 솔루션> = 4.0, "서버에 이미 저장된 파일을 표시하는 방법": https://github.com/enyo/dropzone/wiki/FAQ

maxFiles: 1,

init: function () {
    this.on('maxfilesexceeded', function (file) {
        this.removeAllFiles();
        this.addFile(file);
    });

    var mocks = $dropzone.data('dropzone');
    for (var i = 0; i < mocks.length; i++) {
        var mock = mocks[i];
        mock.accepted = true;

        this.files.push(mock);
        this.emit('addedfile', mock);
        this.createThumbnailFromUrl(mock, mock.url);
        this.emit('complete', mock);
    }
}

위의 punky의 탁월한 답변을 바탕으로 this._updateMaxFilesReachedClass();마지막에 다음과 같이 추가하는 것을 잊지 마십시오 .

init: function () {
    var mockFile = { name: <filename>, size: <filesize>, type: <filetype>, url: <file_url> };
    this.files.push(mockFile);
    this.emit('addedfile', mockFile);
    this.createThumbnailFromUrl(mockFile, mockFile.url);
    this.emit('complete', mockFile);
    this._updateMaxFilesReachedClass();
}

이 답변 https://stackoverflow.com/a/17763511 에서는 썸네일 이벤트 를 내보내는 것으로 구현되었습니다 .

Following is an example of doing it using createThumbnailFromUrl.

HTML element;

<form id="sample-img" action="/upload" class="dropzone">
    <div class="dz-default dz-message"></div>
</form>

JS code;

previewThumbailFromUrl({
    selector: 'sample-img',
    fileName: 'sampleImg',
    imageURL: '/images/sample.png'
});

function previewThumbailFromUrl(opts) {
    var imgDropzone = Dropzone.forElement("#" + opts.selector);
    var mockFile = {
        name: opts.fileName,
        size: 12345,
        accepted: true,
        kind: 'image'
    };
    imgDropzone.emit("addedfile", mockFile);
    imgDropzone.files.push(mockFile);
    imgDropzone.createThumbnailFromUrl(mockFile, opts.imageURL, function() {
        imgDropzone.emit("complete", mockFile);
    });
}

Working Samples on JSFiddle:

  1. Load images on same domain
  2. Load images with crossOrigin

I was having hard time with this also. This one as a starting point would have helped even more:

Dropzone.autoDiscover = false; // otherwise will be initialized twice
var myDropzoneOptions = {
    maxFilesize: 5,
    addRemoveLinks: true,
    clickable: true
};
var myDropzone = new Dropzone('#myDropzoneElement', myDropzoneOptions);
var mockFile = { name: "Existing file!", size: 12345 };
myDropzone.options.addedfile.call(myDropzone, mockFile);
myDropzone.options.thumbnail.call(myDropzone, mockFile, "http://url-to-thumb-goes-here");

The parameters of createThumbnailFromUrl has changed in Version > 5. To make it work again, check out this GitHub Issue: https://github.com/enyo/dropzone/issues/1587#issuecomment-324023260


 var mockFile = { name: "banner2.jpg", size: 12345, uuid: "085b2b23-70e7-4175-8355-89937d8d46f2" };
 myDropzone.emit("addedfile", mockFile);
 myDropzone.options.thumbnail.call(myDropzone, mockFile, "https://your-thumbnail-image.jpg");

On Init-Function of the Dropzone-Object init: function() { call this:

  this.on("addedfile", function(file) {
                        //Access the preview element with file.previewElement and add event listeners, etc... to it.
                        var a = document.createElement('a');
                        a.setAttribute('href',"{{{protokoll}}}://{{{HTTP_HOST}}}/a-getfiledb.php?uuid="+file.uuid);
                        a.setAttribute('class',"btn btn-success");
                        a.setAttribute('style',"width:50%; margin-top:5px; border-left:1px solid black; cursor:pointer;");
                        a.innerHTML = "<i class='glyphicon glyphicon-download-alt'></i>";
                        file.previewTemplate.appendChild(a);
  });

You can try with this

var file_image = "http://someserver.com/myimage.jpg";

var mockFile = { name: "myimage.jpg", size: 12345 };

$("#dropzone").dropzone({

    url: 'false',
    maxFiles: 1,
    maxFilesize:10,//mb
    acceptedFiles:'image/*',
    init: function() {
        this.on("addedfile", function(file){
            this.options.thumbnail.call(this,file,file_image);
         });
         this.addFile.call(this,mockFile);
    }
});

ReferenceURL : https://stackoverflow.com/questions/17759286/how-can-i-show-you-the-files-already-stored-on-server-using-dropzone-js

반응형