将图像从一个.jsp传递到另一个.jsp

时间:2016-11-16 15:32:20

标签: javascript html jsp

我正在学习JSP,但我无法找到以下问题的答案:

我想将图片文件上传到一个页面,将其传递给另一个页面并在那里显示(基于this问题):

first.jsp

<form method="post" onsubmit="save()" action="second.jsp" enctype="multipart/form-data">
    <input type="file" name="openFile" onchange="reloadPreview()">
    <canvas name="preview"></canvas>
    <input type="submit" value="Save"/>
</form>
<script>
    function reloadPreview(){
        var preview = document.getElementsByName("preview")[0];
        var file    = document.getElementsByName("openFile")[0].files[0];
        var reader  = new FileReader();
        var picture = new Image;

        reader.onloadend = function () {
            picture.src = reader.result;
            preview.width = picture.width;
            preview.height = picture.height;
            var context = preview.getContext("2d");
            context.drawImage(picture, 0, 0);
            dataUrl = preview.toDataURL();
        };

        if (file) {
            reader.readAsDataURL(file);
        }
   }

    function save() {
        window.location = "second.jsp?imgUrl="+dataUrl;
        document.getElementById("form").submit();
    }

   var dataUrl;
</script>

1)如何从second.jsp访问画布内容?

2)我是否可以访问openFile文件而无需将其上传到canvas(例如,如果文件不是图片)?

1 个答案:

答案 0 :(得分:-1)

来源:

<%session.setAttribute("Down1","download.png");%>
<td>Image: </td>
<td><img src="download.png" width="516" height="516" alt="Down" name="Down1"/></td>

目的地:

<td><img src="download.png" width="516" height="516" alt="Down" name="Down1"/></td>