如何从Java调用Dropzone(JS)?

时间:2017-08-09 18:23:24

标签: javascript java dropzone.js htmlunit

我正在做一些网络报废任务,其中一个页面包含通过Dropzone上传一些照片的选项。

我的报废是从Java + HTMLUnit完成的,但是我一直处于向JS Dropzone组件添加照片的过程中。如何调用代码来上传文件?

我尝试了不同的选项,但我对Java + HTML + JS交互很新。这不起作用(未定义):

ScriptResult result = currentPage.executeJavaScript("Dropzone.addFile()"); 
    result.getNewPage();

这显示了很多关于方法的信息,但是没有显示与下面代码中包含的脚本有很多相关性(例如,函数版本),而是仅显示在此代码上方定义的方法(用于管理来自Web的cookie和一些信息消息) ):

ScriptResult result = currentPage.executeJavaScript(""
            + "for (var id in this) {"
            + "try {"
            + "console.log(id + \": \" + this[id].toString());"
            + "} catch (err) {"
            + "}"
            + "}");

    webClient.waitForBackgroundJavaScript(600000);

提前致谢,

这是我的HTML重要代码:

<div class="paso">
    <img width="40" height="40" src="https://static.milanuncios.com/imagenes/4-anuncios.gif" title="INSERTAR ANUNCIOS">
    Por último, si lo deseas puedes insertar fotos.
    <br>Los anuncios con foto llaman más la atención de posibles compradores.
</div>

<script src="https://static.milanuncios.com/js/fotos/dropzone.4.2.0.min.js" type="text/javascript"></script>
<script src="https://static.milanuncios.com/js/fotos/exif.js" type="text/javascript"></script>
<script src="https://static.milanuncios.com/js/fotos/dropzone-exif-extension.js" type="text/javascript"></script>
<link rel="stylesheet" href="https://static.milanuncios.com/css/fotos/dropzone.min.css">

<script type="text/javascript">
    function ver(id) {
        document.getElementById(id).style.position = 'absolute';
        document.getElementById(id).style.display = 'inline';
        document.getElementById(id).style.visibility = "visible";
        document.getElementById(id).style.zIndex = '10000';
    }

    Dropzone.options.photoUploader = {
        init: function() {
            var myDropzone = this;
            this.__photoId = 0;
            this.__uploadingPhotos = false;
            this.__isPhotoUploadDone = false;
            this.__errorMessagePrevious = "";

            this.on("thumbnail", function(file){
                ExifExtension.applyRotation(file);
            });

            this.on("success", function() {
                if(this.__uploadingPhotos)
                    this.processQueue();
            });

            this.options.url = function(files) {
                if (files.length !== 1) {
                    alert("Error interno 5690, por favor intentalo más tarde");
                    return;
                }

                var localPhotoId = ++myDropzone.__photoId;

                                    return "/images/upload-image-auth.php?&idanuncio=239861376&clave=V1hEUQ%3D%3D&numfoto="+localPhotoId;
            };

            this.on("processing", function() {
                ver('espera');
                document.getElementById('upload-all').disabled = true;
            });

            this.on("drop", function() {
                this.__errorMessagePrevious = "";
            });

            this.on("error", function(file, errorMessage) {
                console.log("error: ", errorMessage);
                if (file.status == "error") {
                    if (!file.accepted) {
                        this.removeFile(file);
                    } else {
                        errorMessage = this.options.dictResponseError;
                    }
                    if (errorMessage != this.__errorMessagePrevious) {
                        this.__errorMessagePrevious = errorMessage;
                        alert(errorMessage);
                    }
                }
                if(this.__uploadingPhotos)
                    this.processQueue();
            });

            this.on("queuecomplete", function() {
                this.__isPhotoUploadDone = true;
                window.location = 'http://www.milanuncios.com/publicado/?idanuncio=239861376&contra=WXDQ';
            });

            var submitButton = document.getElementById("upload-all");
            submitButton.addEventListener("click", function() {
                myDropzone.__uploadingPhotos = true;
                if (myDropzone.getQueuedFiles().length == 0) {
                    myDropzone.emit("queuecomplete", "");
                }
                else {
                    ver('espera');
                    myDropzone.processQueue();
                }
            });

            var dropArea = document.getElementById("dropArea");
            dropArea.addEventListener("click", function() {
                // Clean all the previous error messages
                myDropzone.__errorMessagePrevious = "";
            });
        },
        paramName: "uploadFile1",
        parallelUploads: 1,
        maxFiles: 9,
        uploadMultiple: false,
        autoProcessQueue: false, // Prevents Dropzone from uploading dropped files immediately
        addRemoveLinks: true,
        maxThumbnailFilesize: 5,
        maxFilesize: 5,
        thumbnailWidth: 100,
        thumbnailHeight: 100,
        imageWidth: 800,
        imageHeight: 800,
        acceptedFiles: "image/jpeg, image/gif ,image/png",
        imageQuality: 0.8,
        dictDefaultMessage: "<strong>Arrastra tus fotos aquí <br/> o <br/> selecciónalas de tu equipo</strong><div class=\"btnAddPhoto\">Añadir fotos</div>",
        dictFallbackMessage: "Tu navegador no soporta arrastrar fotos directamente.",
        dictFallbackText: "Utiliza el método antiguo para subir fotos.",
        dictFileTooBig: "La fotos es demasiado grande.",
        dictInvalidFileType: "Formato no permitido. Asegúrate que tus fotos son JPG, JPEG, GIF o PNG.",
        dictResponseError: "Error al subir tus fotos. Por favor, inténtalo de nuevo más tarde.",
        dictCancelUpload: "Cancelar",
        dictCancelUploadConfirmation: "¿Estás seguro que deseas cancelar la subida?",
        dictRemoveFile: "Eliminar",
        dictRemoveFileConfirmation: "¿Estás seguro que deseas eliminar esta foto?",
        dictMaxFilesExceeded: "No puedes subir más de 9 fotos."
    };

    window.onbeforeunload = function() {
        if (Dropzone && Dropzone.instances[0] && !Dropzone.instances[0].__isPhotoUploadDone) {
            return "¿Realmente quieres irte sin subir tus fotos?";
        }
    };
</script>

<div class="insertadonCol">
    <div class="photoUploaderWrapper">
        <div class="subida" id="espera">
            <br><br>
            Espere por favor...<br><br>
            <img src="https://static.milanuncios.com/imagenes/waiting.gif">
            <br><br>
            <div class="normal">
                Este proceso, puede tardar unos segundos.
                <br>
                <br>
                Gracias,
                <br>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MilAnuncios.
            </div>
        </div>

        <div id="dropArea">
            <form action="/images/upload-image-auth.php" method="post" class="dropzone dz-clickable" drop-zone="" id="photoUploader" enctype="multipart/form-data"><div class="dz-default dz-message"><span><strong>Arrastra tus fotos aquí <br> o <br> selecciónalas de tu equipo</strong><div class="btnAddPhoto">Añadir fotos</div></span></div></form>
        </div>

        <br><br>

        <div class="uploadBtnBox">
            <button id="upload-all" class="uploadBtn">Finalizar publicación</button>
        </div>
    </div>
</div>

0 个答案:

没有答案