【问题标题】:grails asynchronous file uploadgrails 异步文件上传
【发布时间】:2012-04-14 01:58:57
【问题描述】:

在 grails 中是否存在真正的异步/ajax 文件上传,它挂接到默认的“忙碌”微调器(显示在其他 ajax 调用上的微调器)?

或者如果我想在文件上传期间显示微调器,我需要显式调用吗?

【问题讨论】:

    标签: grails asyncfileupload


    【解决方案1】:

    这是您通常希望从第三方获得帮助的事情之一,因为交换背后的一些复杂性和限制。

    一些插件可用于 jquery,它们使用 Flash 来完成此操作,但我更愿意避免使用任何使用 Flash 的东西(个人喜好)。查看Valums Ajax-Upload 我已经在我的生产站点使用了大约一年,它运行良好且易于使用。我最喜欢这个插件的一点是它不会影响大多数网站的设计和布局。

    此外,这是一个常见问题。看看这些答案...

    【讨论】:

    【解决方案2】:

    杰夫,

    我会这样编码...

    1. 仅获取用户的文件路径。
    2. 将文件路径发送到 fileupload 闭包/方法,并在异步块中进行文件读取和保存。
    3. 您可能需要使用 Executor plugin 来实现异步功能。
    4. 在这种情况下,您不需要显示微调器图像。因为提交文件路径很简单,响应也很快。

    如果您想在代码中显示自己,则必须调用微调器。如果要显示图像。

    【讨论】:

    • 这种方法不会立即向用户提供反馈,并允许他们在第一个文件完成上传之前添加另一个文件。
    【解决方案3】:

    您可以使用 XMLHttpRequest,这是我通过拖放完成的项目的完整代码:


    HTML

    <div id="drop_zone">Drag and drop your file here or click</div>
    <input id="curriculumUploader" type="file" name="curriculum" style="display:none;"/>
    

    JAVASCRIPT

        function uploadCurriculum(file) {
            var xmlhttp;
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
    
            //FormData will contain the post params
            var form = new FormData();
    
            xmlhttp.onreadystatechange = function() {
                //Handle response if everything is right
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    //This is the element where i will put my response
                    var element = document.getElementById('curriculum');
                    //The response is given in the property responseText
                    element.innerHTML = xmlhttp.responseText;
                    //If you response has javascript elements, you need to place them in the header to make client compile them
                    var scriptElements = element.getElementsByTagName('SCRIPT');
                    for (i = 0; i < scriptElements.length; i ++) {
                        var scriptElement = document.createElement('SCRIPT');
                        scriptElement.type = 'text/javascript';
                        if (!scriptElements[i].src) {
                            scriptElement.innerHTML = scriptElements[i].innerHTML;
                        } else {
                            scriptElement.src = scriptElements[i].src;
                        }
                        document.head.appendChild(scriptElement);
                    }
                }
            }
            //Put your params in the url if you want to send them by GET
            xmlhttp.open("POST", window.appContext + "/controller/action/" + someParams, true);
            xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
            //add the file to the form to send by POST
            form.append('curriculum', file);
            xmlhttp.send(form);
        }
    
        function handleFileSelect(event) {
            handleDragOver(event);
    
            var files = event.target.files || event.dataTransfer.files;
    
            if (files.length > 0) {
                parseFile(files[0]);
            }
        }
    
        function handleDragOver(event) {
            event.stopPropagation();
            event.preventDefault();
            if (event.type == "dragover") {
                event.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
                addClass(event.target, "hover");
            } else {
                removeClass(event.target, "hover");
            }
        }
    
        function showFileSelect() {
            document.getElementById('curriculumUploader').click();
        }
    
        function parseFile(file) {
            if (file != null && file.size > 0) {
                uploadCurriculum(file)
            } else {
                console.log('Invalid file');
            }
        }
    
        // Setup the dnd listeners.
        var dropZone = document.getElementById('drop_zone');
        if (dropZone != null) {
            dropZone.addEventListener('dragover', handleDragOver, false);
            dropZone.addEventListener("dragleave", handleDragOver, false);
            dropZone.addEventListener('drop', handleFileSelect, false);
            dropZone.addEventListener('click', showFileSelect, false);
        }
    
        var inputelement = document.getElementById('curriculumUploader');
        inputelement.addEventListener('change', handleFileSelect, false);
    

    CSS

        #drop_zone {
            padding: 15px;
            text-align: center;
            border: 1px dashed #4b9df2;
            color: #4b9df2;
            cursor: pointer;
            transition: all linear 0.2s;
        }
    
        #drop_zone.hover {
            border: 1px dashed #C99F23;
            color: #C99F23;
        }
    

    【讨论】:

    • FormData 不支持 IEdeveloper.mozilla.org/en-US/docs/Web/API/FormData
    猜你喜欢
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多