#13 download files

merge-requests/35/head
Семенова Мария 5 years ago
parent 8dc5eec35c
commit 1425cceb63

@ -13,6 +13,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import static java.nio.charset.StandardCharsets.UTF_8;
@ -39,7 +40,7 @@ public class FileService {
}
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());
String meta = multipartFile.getOriginalFilename() + "\n" + multipartFile.getSize();
Files.write(getTmpFileMetaPath(tmpFileName), meta.getBytes(UTF_8));

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

@ -107,8 +107,17 @@
<span aria-hidden="true"><i class="fa fa-times"/></span>
</a>
</div>
<div class="col-10">
<div th:text="*{files[__${rowStat.index}__].name}"></div>
<div class="col-10" th:if="${file.id==null}">
<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>
</th:block>
@ -251,12 +260,43 @@
var nameDiv = $("<div/>")
.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);
$("#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>
</div>

Loading…
Cancel
Save