javascript - 重定向在 springboot Controller 中不起作用

标签 javascript java spring-boot spring-mvc

我正在使用 javascript 制作一个上传图像的表单,这是唯一在应用程序中不起作用的重定向,我还没有找到更好的方法来上传图像,我想将其存储在数据库。我需要该重定向才能工作。

这是 html 代码(我没有使用 thymeleaf 的对象发布):

<form id="singleUploadForm" name="singleUploadForm" class="form-horizontal" th:object="${publication}">
...
<div class="form-group">
    <label class="control-label col-sm-2" for="text"
    th:text="#{publication.image}">Imagen (opcional):</label>
        <div class="col-sm-10">
            <input id="singleFileUploadInput" type="file" class="form-control" name="image"/>
        </div>
</div>
<div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
        <button type="submit" class="btn btn-primary" th:text="#{send}">Enviar</button>
    </div>
</div>
</form>

这是我的 Javascript 代码:

'use strict';

var singleUploadForm = document.querySelector('#singleUploadForm');
var singleFileUploadInput = document.querySelector('#singleFileUploadInput');
var titleInput = document.querySelector('#titleInput');
var textInput = document.querySelector('#textInput');

function uploadSingleFile(file, text, title) {
    var formData = new FormData();
    formData.append("file", file);
    formData.append("text", text);
    formData.append("title", title);

    var xhr = new XMLHttpRequest();
    xhr.open("POST", "/uploadFile");

    xhr.onload = function() {
        //console.log(xhr.responseText);
    }

    xhr.send(formData);
    console.log("DONE1");
}

singleUploadForm.addEventListener('submit', function(event){
    var files = singleFileUploadInput.files;
    var text = textInput.value;
    var title = titleInput.value;
    if(files.length === 0) {
        uploadSingleFile(null, text, title);
    }
    else {
        uploadSingleFile(files[0], text, title);
        //event.preventDefault();
        console.log("DONE2");
    }
    console.log("DONE3");
}, true);

还有 Controller :

@RequestMapping(value = "/uploadFile")
public String uploadFile(Principal principal, @RequestParam(value = "file", required=false) MultipartFile image,
        @RequestParam("text") String text, @RequestParam("title") String title) {
    Publication publication = new Publication();

    if (image != null) {
        DBFile dbFile = storageService.storeFile(image);
        publication.setImage(dbFile);
    }

    User user = usersService.getUserByEmail(principal.getName());

    publication.setUser(user);
    publication.setDate(new Date());
    publication.setText(text);
    publication.setTitle(title);

    publicationsService.addPublication(publication);

    return "redirect:/publication/list";
}

如果有任何方法可以将图像添加到 de th:object="{$publication}"我认为它可以解决问题,但是当我尝试时,我总是收到一个空文件。如果我使用字符串保存图像,我会正确收到名称,但无法获取任何形式的数据。

最佳答案

您所期望的实际上并不正确。

对于 Ajax 调用,从 Controller 返回 json 响应,并根据 Controller 返回的响应,您可以重定向到 javascript 代码中的特定 url。请参阅how to redirect Ajax spring

window.location.href = 'http://www.google.com'; //Will take you to Google.

关于javascript - 重定向在 springboot Controller 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60682094/

相关文章:

javascript - 将鼠标悬停在菜单按钮上时要移动的图标

java - Spring Boot + Gradle : take VM options when I run single tests

spring - 拦截器未通过Spring Boot初始化和调用

java - Spring HATEOAS 1.x 上的 ResourceSupport.getId()

javascript - 如何使用 Node JS 驱动程序创建新的 MongoDb 数据库

javascript - 如何在网页上找到按钮

javascript - Highcharts - 显示 noData 叠加层和 x 轴标签

java - 创建唯一对象的列表

java - 忽略 java 类文件

java - Android 中针对不同应用程序的拆分显示