#13 addNewFile js function

This commit is contained in:
Семенова Мария 2019-03-18 18:39:15 +04:00
parent e609b27518
commit 4fb8061653
3 changed files with 65 additions and 10 deletions

View File

@ -3,12 +3,7 @@ package ru.ulstu.file;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import ru.ulstu.configuration.Constants; import ru.ulstu.configuration.Constants;
import ru.ulstu.core.model.response.Response; import ru.ulstu.core.model.response.Response;
@ -51,7 +46,7 @@ public class FileController {
} }
@PostMapping("/uploadTmpFile") @PostMapping("/uploadTmpFile")
public Response<String> upload(@RequestParam("file") MultipartFile multipartFile) throws IOException { public Response<Object[]> upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
return new Response(fileService.uploadToTmpDir(multipartFile)); return new Response(new Object[]{fileService.uploadToTmpDir(multipartFile), multipartFile.getOriginalFilename()});
} }
} }

View File

@ -33,6 +33,7 @@ public class FileService {
public FileData createFileFromTmp(String tmpFileName) throws IOException { public FileData createFileFromTmp(String tmpFileName) throws IOException {
FileData fileData = new FileData(); FileData fileData = new FileData();
fileData.setData(getTmpFile(tmpFileName)); fileData.setData(getTmpFile(tmpFileName));
fileData.setSize(getTmpFileSize(tmpFileName));
fileData.setName(getTmpFileName(tmpFileName)); fileData.setName(getTmpFileName(tmpFileName));
fileData.setCreateDate(new Date()); fileData.setCreateDate(new Date());
return fileRepository.save(fileData); return fileRepository.save(fileData);

View File

@ -92,7 +92,7 @@
class="alert alert-danger">Incorrect title</p> class="alert alert-danger">Incorrect title</p>
</div> </div>
<div class="form-group"> <div class="form-group" id="files-list">
<label>Файлы:</label> <label>Файлы:</label>
<th:block th:each="file, rowStat : *{files}"> <th:block th:each="file, rowStat : *{files}">
<div class="row" th:id="|files${rowStat.index}|" <div class="row" th:id="|files${rowStat.index}|"
@ -100,6 +100,7 @@
<input type="hidden" th:field="*{files[__${rowStat.index}__].id}"/> <input type="hidden" th:field="*{files[__${rowStat.index}__].id}"/>
<input type="hidden" th:field="*{files[__${rowStat.index}__].deleteFlag}"/> <input type="hidden" th:field="*{files[__${rowStat.index}__].deleteFlag}"/>
<input type="hidden" th:field="*{files[__${rowStat.index}__].name}"/> <input type="hidden" th:field="*{files[__${rowStat.index}__].name}"/>
<input type="hidden" th:field="*{files[__${rowStat.index}__].tmpFileName}"/>
<div class="col-2"> <div class="col-2">
<a class="btn btn-danger float-right" <a class="btn btn-danger float-right"
th:onclick="|$('#files${rowStat.index}\\.deleteFlag').val('true'); $('#files${rowStat.index}').hide(); |"> th:onclick="|$('#files${rowStat.index}\\.deleteFlag').val('true'); $('#files${rowStat.index}').hide(); |">
@ -111,6 +112,7 @@
</div> </div>
</div> </div>
</th:block> </th:block>
</div> </div>
<div class="form-check"> <div class="form-check">
@ -189,15 +191,72 @@
div: "loader", div: "loader",
url: urlFileUpload, url: urlFileUpload,
maxSize: 1.5, maxSize: 1.5,
extensions: ["doc", "docx", "xls", "jpg", "pdf", "txt", "png"], extensions: [],
callback: function (response) { callback: function (response) {
showFeedbackMessage("Файл успешно загружен"); showFeedbackMessage("Файл успешно загружен");
console.debug(response); console.debug(response);
addNewFile(response);
} }
}); });
$('.selectpicker').selectpicker(); $('.selectpicker').selectpicker();
}); });
/*]]>*/ /*]]>*/
function addNewFile(fileNames) {
var filesList = $("#files-list");
var fileNumber = $("#files-list div.row").length;
var newFileRow = $("<div/>")
.attr("id", 'files' + fileNumber)
.addClass("row");
var idInput = $("<input/>")
.attr("type", "hidden")
.attr("id", "files" + fileNumber + ".id")
.attr("value", '')
.attr("name", "files[" + fileNumber + "].id");
newFileRow.append(idInput);
var flagInput = $("<input/>")
.attr("type", "hidden")
.attr("id", "files" + fileNumber + ".deleteFlag")
.attr("value", "false")
.attr("name", "files[" + fileNumber + "].deleteFlag");
newFileRow.append(flagInput);
var nameInput = $("<input/>")
.attr("type", "hidden")
.attr("id", "files" + fileNumber + ".name")
.attr("value", tmpFileName[1])
.attr("name", "files[" + fileNumber + "].name");
newFileRow.append(nameInput);
var tmpFileNameInput = $("<input/>")
.attr("type", "hidden")
.attr("id", "files" + fileNumber + ".tmpFileName")
.attr("value", tmpFileName[0])
.attr("name", "files[" + fileNumber + "].tmpFileName");
newFileRow.append(tmpFileNameInput);
var nextDiv = $("<div/>")
.addClass("col-2");
var nextA = $("<a/>")
.addClass("btn btn-danger float-right")
.attr("onclick", "$('#files" + fileNumber + "\\\\.deleteFlag').val('true'); $('#files" + fileNumber + "').hide();")
.append(($("<span/>").attr("aria-hidden", "true")).append($("<i/>").addClass("fa fa-times")))
;
nextDiv.append(nextA)
newFileRow.append(nextDiv);
var nameDiv = $("<div/>")
.addClass("col-10")
.append($("<div/>").text(tmpFileName[1]));
newFileRow.append(nameDiv);
$("#files-list").append(newFileRow);
}
</script> </script>
</div> </div>