国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

springboot+微信小程序?qū)崿F(xiàn)文件上傳下載(預覽)pdf文件

這篇具有很好參考價值的文章主要介紹了springboot+微信小程序?qū)崿F(xiàn)文件上傳下載(預覽)pdf文件。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

實現(xiàn)思路:

  1. 選擇文件 wx.chooseMessageFile ,官方文檔: https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.chooseMessageFile.html
  2. 上傳文件 `wx,uploadFile` , 官方文檔:https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/wx.uploadFile.html
  3. 查看所有上傳的pdf文件,顯示在頁面上
  4. 點擊pdf文件就實現(xiàn)預覽

5.1 先下載 wx.downloadFile 官方文檔:https://developers.weixin.qq.com/miniprogram/dev/api/network/download/wx.downloadFile.html

5.2 在查看 wx.openDocument 官方文檔:https://developers.weixin.qq.com/miniprogram/dev/api/file/wx.openDocument.html

小程序端wxml

<!--pages/uploadPDF/uploadPDF.wxml-->
<view>
  <view>
    <button type="primary" bindtap="uploadPDF">上傳pdf文檔</button>
  </view>

  <view style="margin:20% auto;width: 100%;">
    <button bindtap="loadPdfList">加載pdf文檔</button>
    <view style="border: 1px solid black;width: 100%;">
      <view wx:if="{{pdfList.length>0}}">
        <view wx:for="{{pdfList}}" style="margin:5px;display: flex;justify-content: center;align-items: center;">
          <text style="text-overflow: ellipsis; word-wrap: break-word;width: 80%;">{{item}}</text>
          <button size="mini" type="warn" data-src="{{item}}" bindtap="lookPDF">查看</button>
        </view>
      </view>

      <view wx:else style="text-align: center;">
        <text>暫無</text>
      </view>

    </view>
  </view>

</view>

小程序端ts

/**
   * 頁面的初始數(shù)據(jù)
   */
  data: {
    //pdf文檔
    pdfList: {}
  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁面加載
   */
  onLoad() {

  },
  uploadPDF() {
    console.log("uploadPDF")
    wx.chooseMessageFile({
      //最多可以選擇的文件數(shù)量,最多是0-100
      count: 1,
      //指打開文件的類型
      type: "file",
      success: (res) => {
        let path = res.tempFiles[0].path
        let suffix = path.split(".")[1]
        if (suffix != "pdf") {
          console.log("只能上傳pdf的文件")
          return
        }
        wx.uploadFile({
          url: "http://172.16.50.86:8080/upload",
          header: {
            "Content-Type": "multipart/form-data"
          },
          name: "file",
          filePath: path,
          success: (res) => {
            if (res.statusCode == 200) {
              console.log("上傳成功")
            } else {
              console.error(res.data)
            }
          }
        })
      }
    })
  },
  //加載pdf列表
  loadPdfList() {
    wx.request({
      url: "http://172.16.50.86:8080/fileList",
      method: "GET",
      success: (res) => {
        this.setData({
          pdfList: res.data
        })
      }
    })
  },
  //查看pdf文檔
  lookPDF(e) {
    wx.showLoading({
      title:"加載中...",
      mask:true
    })
    let imgUrl = e.currentTarget.dataset.src
    wx.downloadFile({
      url: "http://172.16.50.86:8080/download?file=" + imgUrl,
      success: (res) => {
        if (res.statusCode === 200) {
          setTimeout(() => {
            wx.openDocument({
              filePath:res.tempFilePath,
              fileType:"pdf"
            })
            wx.showToast({
              title:"加載成功!.",
              icon:"success",
              duration:2000,

            })
            wx.hideLoading()
          }, 1500);
         }

      },fail(){
        wx.hideLoading()
      }
    })
  },

后端

1.1 導入相關(guān)的maven依賴

<!--文件上傳與下載相關(guān)的依賴-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.2</version>
        </dependency>

1.2 設(shè)置上傳文件的大小

spring:
  servlet:
    multipart:
      max-file-size: 5MB # 單個文件大小
      max-request-size: 50MB # 總文件大小Apache Commons FIle Upload

1.3 編寫代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-721450.html

package com.demo1.web;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@RestController
public class UploadController {

    /**
     * 文件上傳
     *
     * @param request
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    @CrossOrigin
    public String upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {

        String parameter = request.getParameter("filePath");
        System.out.println("parameter = " + parameter);

        if (!file.isEmpty()) {
            //上傳文件路徑
//            String path = request.getServletContext().getRealPath("/uploadFiles/");
            String path = "C:/upload";
            //獲得上傳文件名
            String fileName = file.getOriginalFilename();
            File filePath = new File(path + File.separator + fileName);

            System.out.println(filePath);
            //如果文件目錄不存在,創(chuàng)建目錄
            if (!filePath.getParentFile().exists()) {
                filePath.getParentFile().mkdirs();
            }
            //將上傳文件保存到一個目標文件中
            file.transferTo(filePath);
            return fileName;
        }
        return "fail";
    }

    /**
     * 文件下載
     *
     * @param request
     * @param fileName
     * @return
     * @throws IOException
     */
    @GetMapping("download")
    @CrossOrigin
    public ResponseEntity<byte[]> download(HttpServletRequest request, @RequestParam("file") String fileName) throws IOException {
        //下載文件路徑
        String path = "C:/upload";
        //構(gòu)建將要下載的文件對象
        File file = new File(path + File.separator + fileName);
        //設(shè)置響應頭
        HttpHeaders headers = new HttpHeaders();
        //下載顯示的文件名,解決中文名稱亂碼問題
        String downloadFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        //通知瀏覽器以下載方式(attachment)打開文件
        headers.setContentDispositionFormData("attachment", downloadFileName);
        //定義以二進制流數(shù)據(jù)(最常見的文件下載)的形式下載返回文件數(shù)據(jù)
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //使用spring mvc框架的ResponseEntity對象封裝返回下載數(shù)據(jù)
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
    }

    @GetMapping("/fileList")
    @CrossOrigin
    public List<String> list(HttpServletRequest request) {
        String path = "C:/upload";
        File file = new File(path);

        List<String> arrayList = new LinkedList<>();
        for (String list : file.list()) {
            String str = list;

            String split = list.substring(list.indexOf(".") + 1);
            if (split.equals("pdf")) {
                arrayList.add(list);
            }
        }
        return arrayList;
    }


    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

}

到了這里,關(guān)于springboot+微信小程序?qū)崿F(xiàn)文件上傳下載(預覽)pdf文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • 微信小程序中pdf的上傳、下載及excel導出

    上傳兩種方法: 1.用vant weapp組件: 參考:https://blog.csdn.net/weixin_38566069/article/details/110229404 提示:突然冒出一個報錯:wx.chooseMessageFile點擊很多次后就突然無效了 昨天上傳功能在【微信開發(fā)工具和移動端】都可以用,早上突然實現(xiàn)了。 查了下是官方給出的解釋是: 2023年9月

    2024年02月12日
    瀏覽(21)
  • SpringBoot 如何實現(xiàn)文件上傳和下載

    SpringBoot 如何實現(xiàn)文件上傳和下載

    當今Web應用程序通常需要支持文件上傳和下載功能,Spring Boot提供了簡單且易于使用的方式來實現(xiàn)這些功能。在本篇文章中,我們將介紹Spring Boot如何實現(xiàn)文件上傳和下載,同時提供相應的代碼示例。 Spring Boot提供了Multipart文件上傳的支持。Multipart是HTTP協(xié)議中的一種方式,用

    2024年02月15日
    瀏覽(38)
  • SpringBoot整合Hutool實現(xiàn)文件上傳下載

    我相信我們在日常開發(fā)中,難免會遇到對各種媒體文件的操作,由于業(yè)務需求的不同對文件操作的代碼實現(xiàn)也大不相同 maven配置 文件類 文件接口? 配置靜態(tài)資源映射

    2024年02月02日
    瀏覽(26)
  • SpringBoot整合Minio實現(xiàn)文件上傳、下載

    SpringBoot整合Minio實現(xiàn)文件上傳、下載

    SpringBoot整合Minio實現(xiàn)文件上傳、下載: 1,介紹高性能分布式存儲文件服務Minio:Minio是 基于Go語言編寫的對象存儲服務 , 適合于存儲大容量非結(jié)構(gòu)化的數(shù)據(jù) ,例如 圖片、音頻、視頻、日志文件、備份數(shù)據(jù)和容器/虛擬機鏡像等 ,而一個對象文件可以是任意大小,從幾kb到最

    2024年02月06日
    瀏覽(33)
  • SpringBoot+MinIO 實現(xiàn)文件上傳、讀取、下載、刪除

    一、 MinIO 二、 MinIO安裝和啟動 三、 pom.xml 四、 applicatin.properties(配置文件) 五、 編寫Java業(yè)務類

    2024年02月09日
    瀏覽(43)
  • SpringBoot實現(xiàn)文件上傳和下載筆記分享(提供Gitee源碼)

    前言:這邊匯總了一下目前SpringBoot項目當中常見文件上傳和下載的功能,一共三種常見的下載方式和一種上傳方式,特此做一個筆記分享。 目錄 一、pom依賴 二、yml配置文件 三、文件下載

    2024年02月11日
    瀏覽(25)
  • Springboot + MySQL + html 實現(xiàn)文件的上傳、存儲、下載、刪除

    Springboot + MySQL + html 實現(xiàn)文件的上傳、存儲、下載、刪除

    實現(xiàn)步驟及效果呈現(xiàn)如下: 1.創(chuàng)建數(shù)據(jù)庫表: 表名:file_test 存儲后的數(shù)據(jù): 2.創(chuàng)建數(shù)據(jù)庫表對應映射的實體類: import com.baomidou.mybatisplus.annotation.IdType ; import com.baomidou.mybatisplus.annotation. TableField ; import com.baomidou.mybatisplus.annotation. TableId ; import com.baomidou.mybatisplus.annotation. Tab

    2024年04月29日
    瀏覽(27)
  • 一張圖帶你學會入門級別的SpringBoot實現(xiàn)文件上傳、下載功能

    一張圖帶你學會入門級別的SpringBoot實現(xiàn)文件上傳、下載功能

    ?????作者名稱:DaenCode ??作者簡介:啥技術(shù)都喜歡搗鼓搗鼓,喜歡分享技術(shù)、經(jīng)驗、生活。 ??人生感悟:嘗盡人生百味,方知世間冷暖。 ??所屬專欄:SpringBoot實戰(zhàn) 標題 一文帶你學會使用SpringBoot+Avue實現(xiàn)短信通知功能(含重要文件代碼) 一張思維導圖帶你學會Springboot創(chuàng)

    2024年02月12日
    瀏覽(117)
  • 基于SpringBoot實現(xiàn)文件上傳和下載(詳細講解And附完整代碼)

    目錄 一、基于SpringBoot實現(xiàn)文件上傳和下載基于理論 二、詳細操作步驟 文件上傳步驟: 文件下載步驟: 三、前后端交互原理解釋? 四、小結(jié)? 博主介紹:?專注于前后端領(lǐng)域開發(fā)的優(yōu)質(zhì)創(chuàng)作者、秉著互聯(lián)網(wǎng)精神開源貢獻精神,答疑解惑、堅持優(yōu)質(zhì)作品共享。本人是掘金/騰訊

    2024年04月11日
    瀏覽(114)
  • 【java】java實現(xiàn)大文件的分片上傳與下載(springboot+vue3)

    【java】java實現(xiàn)大文件的分片上傳與下載(springboot+vue3)

    源碼: https://gitee.com/gaode-8/big-file-upload 演示視頻 https://www.bilibili.com/video/BV1CA411f7np/?vd_source=1fe29350b37642fa583f709b9ae44b35 對于超大文件上傳我們可能遇到以下問題 ? 大文件直接上傳,占用過多內(nèi)存,可能導致內(nèi)存溢出甚至系統(tǒng)崩潰 ? 受網(wǎng)絡環(huán)境影響,可能導致傳輸中斷,只能重

    2024年02月02日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包