我正在尝试使用“vaadin-upload”实现文件上传功能。我有一个使用 Spring boot 实现的本地构建的文件上传服务器,我想使用它的 API 来上传文件。我想使用这个 API 上传从“vaadin-upload”输入的文件。
API规范是这样的。
- [网址] http://localhost:8080/uploadFile
- [标题] 内容类型:multipart/form-data
- [requestparam] 文件:多部分文件
“ vaadin-upload ”的 API 参考并没有告诉我如何执行 API。我不知道如何运行 API。请告诉我。
索引.html
<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8"/>
<title>sample</title>
<script type="module" src="./out/api-sample/file-upload.js"></script>
<file-upload></file-upload>
文件上传.ts
import {customElement} from 'lit/decorators.js';
import {html, LitElement} from 'lit';
import '@vaadin/vaadin-upload';
@customElement('file-upload')
export class FileUpload extends LitElement {
render() {
return html`
<vaadin-upload></vaadin-upload>
<vaadin-button @click="${this.upload}">upload</vaadin-button>
`;
}
upload() {
// ???
}
}
declare global {
interface HTMLElementTagNameMap {
'file-upload': FileUpload;
}
}
文件控制器.java
package com.example.demo.controller;
import com.example.demo.date.UploadFileResponse;
import com.example.demo.service.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
@RestController
public class FileController {
private final FileStorageService fileStorageService;
@Autowired
public FileController(FileStorageService fileStorageService) {
this.fileStorageService = fileStorageService;
}
@PostMapping("/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
String fileName = fileStorageService.storeFile(file);
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/downloadFile/")
.path(fileName)
.toUriString();
return new UploadFileResponse(fileName, fileDownloadUri, file.getContentType(), file.getSize());
}
}