file download

This commit is contained in:
Captain.B 2020-02-20 17:47:58 +08:00
parent 09a5c11ab0
commit 2c84557bc1
6 changed files with 43 additions and 20 deletions

View File

@ -15,6 +15,9 @@
<if test="request.name != null">
and load_test.name like CONCAT('%', #{request.name},'%')
</if>
<if test="request.workspaceId != null">
AND project.workspace_id = #{request.workspaceId}
</if>
</where>
</select>

View File

@ -9,6 +9,7 @@ import io.metersphere.controller.request.testplan.*;
import io.metersphere.dto.LoadTestDTO;
import io.metersphere.service.FileService;
import io.metersphere.service.LoadTestService;
import io.metersphere.user.SessionUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@ -29,6 +30,7 @@ public class LoadTestController {
@PostMapping("/list/{goPage}/{pageSize}")
public Pager<List<LoadTestDTO>> list(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody QueryTestPlanRequest request) {
Page<Object> page = PageHelper.startPage(goPage, pageSize, true);
request.setWorkspaceId(SessionUtils.getCurrentWorkspaceId());
return PageUtils.setPageInfo(page, loadTestService.list(request));
}
@ -64,12 +66,11 @@ public class LoadTestController {
}
@PostMapping("/file/download")
public ResponseEntity<org.springframework.core.io.Resource> downloadJmx(@RequestBody FileOperationRequest fileOperationRequest) {
org.springframework.core.io.Resource resource = fileService.loadFileAsResource(fileOperationRequest.getName());
public ResponseEntity<byte[]> downloadJmx(@RequestBody FileOperationRequest fileOperationRequest) {
byte[] bytes = fileService.loadFileAsBytes(fileOperationRequest.getId());
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileOperationRequest.getName() + "\"")
.body(resource);
.body(bytes);
}
}

View File

@ -1,8 +1,17 @@
package io.metersphere.controller.request.testplan;
public class FileOperationRequest {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}

View File

@ -2,6 +2,7 @@ package io.metersphere.controller.request.testplan;
public class QueryTestPlanRequest extends TestPlanRequest {
private String name;
private String workspaceId;
@Override
public String getName() {
@ -12,4 +13,12 @@ public class QueryTestPlanRequest extends TestPlanRequest {
public void setName(String name) {
this.name = name;
}
public String getWorkspaceId() {
return workspaceId;
}
public void setWorkspaceId(String workspaceId) {
this.workspaceId = workspaceId;
}
}

View File

@ -38,18 +38,10 @@ public class FileService {
fileMap.put(name, file);
}
public org.springframework.core.io.Resource loadFileAsResource(String name) {
final MultipartFile file = fileMap.get(name);
public byte[] loadFileAsBytes(String id) {
FileContent fileContent = fileContentMapper.selectByPrimaryKey(id);
if (file != null) {
try {
return new InputStreamResource(file.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
return fileContent.getFile();
}
public FileMetadata getFileMetadataByTestId(String testId) {

View File

@ -53,7 +53,7 @@
</template>
<script>
import Message from "element-ui";
import {Message} from "element-ui";
export default {
name: "TestPlanBasicConfig",
@ -91,6 +91,7 @@
});
this.tableData.push({
id: file.id,
name: file.name,
size: file.size + 'Byte', /// todo: ByteKBMB
type: 'JMX',
@ -120,10 +121,16 @@
},
handleDownload(file) {
let data = {
name: file.name
name: file.name,
id: file.id,
};
this.result = this.$post(this.jmxDownloadPath, data, response => {
let config = {
url: this.jmxDownloadPath,
method: 'post',
data: data,
responseType: 'blob'
};
this.result = this.$request(config).then(response => {
const content = response.data;
const blob = new Blob([content]);
if ("download" in document.createElement("a")) {
@ -131,13 +138,15 @@
// chrome/firefox
let aTag = document.createElement('a');
aTag.download = file.name;
aTag.href = URL.createObjectURL(blob)
aTag.href = URL.createObjectURL(blob);
aTag.click();
URL.revokeObjectURL(aTag.href)
} else {
// IE10+
navigator.msSaveBlob(blob, this.filename)
}
}).catch(e => {
Message.error({message: e.message, showClose: true});
});
},
handleDelete(file, index) {