获取图像路径并在TextArea中显示

时间:2016-10-29 16:20:07

标签: javascript html model-view-controller

在我的MVC网络应用程序中,我希望从textarea获取图像路径并显示它。这是我的代码片段,但它不起作用!

     <label>Load Picture</label>&ensp;
            <input id="uploadImage" type="file" name="myPhoto" accept="image/gif, image/jpeg, image/png" class="form-control" onchange="PreviewImage();" />
            <script type="text/javascript">
                function PreviewImage() {
                    var oFReader = new FileReader();
                    oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]);

                    oFReader.onload = function (oFREvent) {
                        document.getElementById("uploadPreview").src = oFREvent.target.result;
                    };
                };
            </script>
            <img id="uploadPreview" style="width: 350px; height: 400px;" class="form-control" /><br />

            <label>Save</label>
            <input type="button" class="btn btn-primary" value="Save" onclick="LoadCuepoint();" />
            <script type="text/javascript">
                function LoadCuepoint() {
                  textArea1.value = path.getElementById("uploadImage");
                };
            </script>

1 个答案:

答案 0 :(得分:0)

您的代码不完整,但我假设您已经分别有文件输入和文本区域。如果是这样,以下应该有效:

<input type="file" onchange="handleFilePath(this);" />
<textarea id="filePathTextArea"></textarea>

文件更改后,您应该能够使用FileReader读取其路径:

function handleFilePath(input) {
    // Initialize file variable
    var file;

    // Check if a file is actually selected
    if (input.files && (file = input.files[0])) {
        // Create a FileReader
        var fileReader = new FileReader();

        // Listen to its onload event
        fileReader.onload = function(e) {
            // Set the result to textarea
            document.getElementById("filePathTextArea").value = e.target.result;
        };

        // Then read the file
        fileReader.readAsDataURL(file);
    }
}