将 Textarea 内容上传为文件

时间:2020-12-25 12:43:30

标签: file-upload textarea

我想将 textarea 的内容作为文件上传,因为内容超出了 php.ini 中的 max_input_vars 限制。将来可能会增加,所以我更喜欢将其作为文件上传^^

我在 textarea 中向用户显示内容,因此它是可编辑的 :) 然后单击“保存”并将 textarea 的内容作为文件发送。我该怎么办?

<span onclick="data_save();">Save</span>

<textarea id="textarea">
    <?php
        print_r($tab);
    ?>
</textarea>
    
<script type="text/javascript">
    <?php
        echo file_get_contents('/var/www/'.$jsdir.'/jquery-2.1.4.min.js');
    ?>
    
    function data_save()
    {
        var textToWrite = document.getElementById("textarea").innerHTML;
        var textFileAsBlob = new Blob([ textToWrite ], { type: 'text/plain' });
        var fileNameToSaveAs = "ecc.plist";

        var downloadLink = document.createElement("a");
        downloadLink.download = fileNameToSaveAs;
        downloadLink.innerHTML = "Download File";
        if (window.webkitURL != null)
        {
            // Chrome allows the link to be clicked without actually adding it to the DOM.
            downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
        } else {
            // Firefox requires the link to be added to the DOM before it can be clicked.
            downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
            downloadLink.onclick = destroyClickedElement;
            downloadLink.style.display = "none";
            document.body.appendChild(downloadLink);
        }
        
        // downloadLink.click();
        
        $.ajax({
            url: 'data_save.php',
            data: data,
            type: 'POST',
            success: function(retour)
            {
                if (retour.indexOf('Oups') != -1)
                    alert(retour);
            },
            error: function(obj, text, error)
            {
                alert("Oups.. " + obj.responseText);
            }
        });
    }
        
</script>```

1 个答案:

答案 0 :(得分:0)

不可能,用户必须自己下载和上传文件....

相关问题