#13 download files

This commit is contained in:
Семенова Мария 2019-03-18 21:02:26 +04:00
parent 8dc5eec35c
commit 1425cceb63
3 changed files with 47 additions and 4 deletions

View File

@ -13,6 +13,7 @@ import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
@ -39,7 +40,7 @@ public class FileService {
} }
public String uploadToTmpDir(MultipartFile multipartFile) throws IOException { public String uploadToTmpDir(MultipartFile multipartFile) throws IOException {
String tmpFileName = String.valueOf(System.currentTimeMillis()); String tmpFileName = String.valueOf(System.currentTimeMillis()) + UUID.randomUUID();
Files.write(getTmpFilePath(tmpFileName), multipartFile.getBytes()); Files.write(getTmpFilePath(tmpFileName), multipartFile.getBytes());
String meta = multipartFile.getOriginalFilename() + "\n" + multipartFile.getSize(); String meta = multipartFile.getOriginalFilename() + "\n" + multipartFile.getSize();
Files.write(getTmpFileMetaPath(tmpFileName), meta.getBytes(UTF_8)); Files.write(getTmpFileMetaPath(tmpFileName), meta.getBytes(UTF_8));

View File

@ -2,6 +2,8 @@
/* global urlVersions */ /* global urlVersions */
var urlFileUpload = "/api/1.0/files/uploadTmpFile"; var urlFileUpload = "/api/1.0/files/uploadTmpFile";
var urlFileDownload = "/api/1.0/files/download/";
var urlFileDownloadTmp = "/api/1.0/files/download-tmp/";
/* exported MessageTypesEnum */ /* exported MessageTypesEnum */
var MessageTypesEnum = { var MessageTypesEnum = {

View File

@ -107,8 +107,17 @@
<span aria-hidden="true"><i class="fa fa-times"/></span> <span aria-hidden="true"><i class="fa fa-times"/></span>
</a> </a>
</div> </div>
<div class="col-10"> <div class="col-10" th:if="${file.id==null}">
<div th:text="*{files[__${rowStat.index}__].name}"></div> <a th:onclick="|downloadFile('${file.tmpFileName}',null,'${file.name}') |"
href="javascript:void(0)"
th:text="*{files[__${rowStat.index}__].name}">
</a>
</div>
<div class="col-10" th:unless="${file.id==null}">
<a th:onclick="|downloadFile(null,${file.id},'${file.name}') |"
href="javascript:void(0)"
th:text="*{files[__${rowStat.index}__].name}">
</a>
</div> </div>
</div> </div>
</th:block> </th:block>
@ -251,12 +260,43 @@
var nameDiv = $("<div/>") var nameDiv = $("<div/>")
.addClass("col-10") .addClass("col-10")
.append($("<div/>").text(fileNames[1])); .append($("<a/>").text(fileNames[1])
.attr("href", 'javascript:void(0)')
.attr("onclick", "downloadFile('" + fileNames[0] + "',null,'" + fileNames[1] + "')"));
newFileRow.append(nameDiv); newFileRow.append(nameDiv);
$("#files-list").append(newFileRow); $("#files-list").append(newFileRow);
} }
function downloadFile(tmpName, fileId, downloadName) {
let xhr = new XMLHttpRequest();
if (fileId != null) xhr.open('GET', urlFileDownload + fileId);
if (tmpName != null) xhr.open('GET', urlFileDownloadTmp + tmpName);
xhr.responseType = 'blob';
var formData = new FormData();
if (fileId != null) formData.append("file-id", fileId);
if (tmpName != null) formData.append("tmp-file-name", tmpName);
xhr.send(formData);
xhr.onload = function () {
if (this.status == 200) {
console.debug(this.response);
var blob = new Blob([this.response], {type: '*'});
let a = document.createElement("a");
a.style = "display: none";
document.body.appendChild(a);
let url = window.URL.createObjectURL(blob);
a.href = url;
a.download = downloadName;
a.click();
window.URL.revokeObjectURL(url);
} else {
}
}
}
</script> </script>
</div> </div>