找回密码
 立即注册

QQ登录

只需一步,快速开始

Winny

超级版主

138

主题

255

帖子

1629

积分

超级版主

Rank: 8Rank: 8

积分
1629
Winny
超级版主   /  发表于:2024-9-14 11:58  /   查看:44  /  回复:0
本帖最后由 Winny 于 2024-9-14 12:12 编辑

背景:SpreadJS作为前端控件,不会直接将文件或者文件中的部分数据保存到数据库中,只是提供获取当前文件或者文件中的部分数据的API,接下来需要采用前后端开发过程中常见的http请求形式,将目标数据传递给服务端,服务端再进行保存。


在SpreadJS中,以文件保存为例,提供了三种文件格式,分别是sjs、ssjson和xlsx文件格式。其中生成ssjson和xlsx调用的api为:
  1. // 将spread导出为xlsx、ssjson、csv文件。
  2. spread.export(function (blob) {
  3.    //这里blob即为生成的excel或ssjson文件blob
  4. }, function (e) {
  5.    console.log(e);
  6. }, {
  7.    fileType: GC.Spread.Sheets.FileType.excel
  8. });
复制代码
接下来我们来看一个前端上传文件的通用代码:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Upload Excel File</title>
  6. </head>
  7. <body>
  8. <h1>Upload Excel File</h1>
  9. <input type="file" id="fileInput" accept=".xlsx">
  10. <button onclick="uploadFile()">Upload</button>

  11. <script>
  12. function uploadFile() {
  13. const fileInput = document.getElementById('fileInput');
  14. const file = fileInput.files[0];     //这里的file其实就是spread.export方法回调中的blob,可以使用new File([blob],"filename.xlsx")将blob转为file对象,也可以直接使用blob

  15. if (!file) {
  16. alert('Please select a file to upload.');
  17. return;
  18. }

  19. const formData = new FormData();
  20. formData.append('file', file);

  21. fetch('/upload', {
  22. method: 'POST',
  23. body: formData
  24. })
  25. .then(response => response.json())
  26. .then(data => {
  27. alert(data.message);
  28. })
  29. .catch(error => {
  30. console.error('Error:', error);
  31. alert('Upload failed.');
  32. });
  33. }
  34. </script>
  35. </body>
  36. </html>
复制代码
不难发现,其实我们只需要将spread中export方法成功回调的对象传递给fetch请求携带的formData即可。同理sjs文件也是类似的,只不过spreadjs中生成sjs文件的形式与生成xlsx文件的方法略有不同。生成sjs文件的代码如下:
  1. // 将spread保存为sjs文件。
  2. spread.save(function (blob) {
  3.    //接下来将blob直接传递给fetch请求的formData即可。
  4.    
  5. }, function (e) {
  6.    console.log(e);
  7. });
复制代码
发送完成后,接下来就是服务端存储了,对于文件,一般采用存储在文件服务器上,数据库直接存文件所在地址即可。我们以java+Spring Boot为例,一个通用的写文件方法如下:
  1. import org.springframework.web.bind.annotation.*;
  2. import org.springframework.web.multipart.MultipartFile;
  3. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  4. import org.springframework.stereotype.Controller;

  5. import java.io.IOException;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;

  9. @Controller
  10. public class FileUploadController {

  11.     @PostMapping("/upload")
  12.     @ResponseBody
  13.     public String singleFileUpload(@RequestParam("file") MultipartFile file,
  14.                                    RedirectAttributes redirectAttributes) {

  15.         if (file.isEmpty()) {
  16.             return "{"message":"Please select a file to upload."}";
  17.         }

  18.         try {
  19.             // Get the file and save it somewhere
  20.             byte[] bytes = file.getBytes();
  21.             Path path = Paths.get("uploads/" + file.getOriginalFilename());
  22.             Files.write(path, bytes);

  23.             redirectAttributes.addFlashAttribute("message",
  24.                     "You successfully uploaded '" + file.getOriginalFilename() + "'");

  25.         } catch (IOException e) {
  26.             e.printStackTrace();
  27.             return "{"message":"Upload failed."}";
  28.         }

  29.         return "{"message":"Upload successful."}";
  30.     }
  31. }
复制代码
代码其实很简单,文件上传属于前后端常见交互请求,我们唯一要做的就是将SpreadJS生成的文件做好传递即可。


0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部