简体   繁体   English

重定向在 springboot 控制器中不起作用

[英]Redirect isn't working in springboot controller

I am using javascript to make a form that uploads an image and this is the only redirect that doesn't work in the application, I haven't found a better way to upload the image and I want to store it in a database.我正在使用 javascript 制作一个上传图像的表单,这是唯一一个在应用程序中不起作用的重定向,我还没有找到更好的上传图像的方法,我想将它存储在数据库中。 I need that redirect to work.我需要重定向才能工作。

Here it is the html code (I'm not using the object publication from thymeleaf):这是 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>

And here it is my Javascript code:这是我的 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);

Also the 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";
}

If there is any way to add the image to de th:object="{$publication}" I think it would solve the problem, but when I tried that, I always received a null file.如果有任何方法可以将图像添加到 de th:object="{$publication}" 我认为它会解决问题,但是当我尝试这样做时,我总是收到一个空文件。 If I use a String to save the image, I recieve the name correctly but I couldn't obtain the data in any form.如果我使用字符串来保存图像,我会正确收到名称,但无法以任何形式获取数据。

What you are expecting is not correct actually.您所期望的实际上并不正确。

For the Ajax call, return json response from the controller and based on the response returned by controller you can redirect to specific url in your javascript code.对于 Ajax 调用,从控制器返回 json 响应,根据控制器返回的响应,您可以重定向到 javascript 代码中的特定 url。 Refer how to redirect Ajax spring参考如何重定向 Ajax spring

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM