реализация функции загрузки файла весной с помощью ajax-запроса для файлов, хранящихся в виде двоичных двоичных данных - PullRequest
0 голосов
/ 07 сентября 2018

Я пытаюсь разработать функционал для загрузки файлов весной с помощью вызовов ajax. Я знаю, что уже было задано много вопросов, и я подробно рассмотрел их и нашел следующие, но загрузка файла еще не работает.

Мой вызов ajax в Ext.js:

downloadFile:function(){
        var me = this,
            constant = Kynox.constant;
        console.log("hello");
        Ext.Ajax.request({
            url: "/protected/impoccurences/downloadSelectedOccurence.do",
            method: 'GET',
            params: {
                occurenceId: me.getSelectedRecord().getId()
            },
            success: function(response){
                console.log("success: " + response.responseText);
                console.log(new Blob([response]));
            },
            failure: function(response, opts){
                console.log("server-side failure with status code: " + response.status);
            }
        });
}

и контроллер:

@RequestMapping(value="/protected/impoccurences/downloadSelectedOccurence", method=RequestMethod.GET)
public ModelAndView downloadSelectedFile(HttpServletRequest request, HttpServletResponse response) {
        ServletOutputStream outputStream = null;
        try {
            int occurenceId = ServletRequestUtils.getRequiredIntParameter(request, "occurenceId");
            ImpOccurence impOccurence = (ImpOccurence) service.selectSource(Integer.toString(occurenceId));
            byte[] fileContent = impOccurence.getSourceFileOther();//this retrieves the data
            String fileName = impOccurence.getFileName();
            String fileType = "." + FilenameUtils.getExtension(fileName.toLowerCase());
            DocContentType docContentType = (DocContentType) docContentTypeDao.findBy("Extension", fileType);

            outputStream = response.getOutputStream();
            String contentType = docContentType.getContentType();
            response.setContentType(docContentType==null?null:contentType);

            response.setHeader("Content-disposition","attachment; filename=\""+fileName+"\"");
            outputStream.write(fileContent);

            outputStream.flush();
            outputStream.close();
            return null;
        }catch(Exception ex){
            daoLogger.error(ex.getMessage(), ex);
            throw new RuntimeException(ex);
        }finally {
            try {
                if(outputStream != null) {
                    outputStream.close();
                }
            }catch(Exception ex) {
                daoLogger.error("Download error during closing resources: " + ex);
            }
        }
}

и вот что я получаю в консоли:

hello
success: stuffer n�2
Blob(15) {size: 15, type: ""}

как я могу заставить это работать? Заранее спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...