一對一直播平臺原始碼,如何實現圖片釋出

雲豹科技曉彤發表於2021-12-22

圖片釋出是一對一直播平臺原始碼動態釋出的功能之一,那麼圖片釋出是怎樣實現的呢?

圖片上傳和圖片載入(靜態釋出)

先建立個靜態資源模組

   <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

 

server.port=8899

spring.web.resources.static-locations=file:D:/resource

 

 

這裡可透過路徑訪問檔案中資訊
如:



實現 檔案上載 和顯示

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title> 檔案上載 </title>

    <script src="bower_components/jquery/dist/jquery.js"></script>

</head>

<body>

<form id="demoForm" method="post"  enctype="multipart/form-data" action="/upload/file">

    <div>

        <label> 上載檔案:

            <input id="imageFile" type="file" name="imageFile">

        </label>

    </div>

    <input type="submit" value=" 提交 ">

</form>

<img src="" id="image">

<script   src="js/utils.js"></script>

<script>

    $(function () {

        // 繫結表單提交事件

        $("#demoForm").submit(function(){

            // 找到檔案控制元件

            let files = document.getElementById("imageFile").files;

 

            if (files.length>0){

                let file = files[0]

                console.log(file);

                upLoadImage(file);

            }else {

                alert(" 請選擇檔案 ");

            }

            return false;

        });

        function upLoadImage(file){

            // 建立上載表單資料物件

            let form = new FormData();

            form.append("imageFile",file);

            $.ajax({

                url:"/upload/file",

                method:"POST",

                data:form,

                contentType:false,

                processData:false,

                success:function(r){

                    if (r.code===OK){

                        console.log(r.message);

                        $("#image").attr("src",r.message)

                    }else{

                        alert(r.message)

                    }

                }

            })

        }

    })

</script>

</body>

</html>

 

   @Value("${straw.redource.path}")

  private   File resourcePath;

    @Value("${straw.resource.host}")

    private String resourceHost;

 @PostMapping("/upload/file")

    public R uploadFile(MultipartFile imageFile) throws IOException {

 

 

        // 按照時間建立資料夾

        String path = DateTimeFormatter.ofPattern("yyyy/MM/dd")

                        .format(LocalDate.now());

        File folder  = new File(resourcePath,path);

        folder.mkdirs();

        log.debug(" 上傳資料夾為: {}",folder.getAbsolutePath());

        // 獲取原始檔名進行擷取

        String fileName = imageFile.getOriginalFilename();

        // 擷取字尾

        String ext = fileName.substring(fileName.lastIndexOf("."));

        // 建立隨即檔名

        String name = UUID.randomUUID().toString()+ext;

        File file = new File(folder,name);

        // 把內容載入到檔案裡面

        imageFile.transferTo(file);

        // 獲取檔案儲存 URL

        String url =  resourceHost+"/"+path+"/"+name;

        log.debug(" 訪問 URL 為: {}",url);

        return R.ok(url);

 

    }

}

 

straw.redource.path=file:D:/resource

straw.resource.host=

 

summernote上傳圖片

<div>

          <!-- 富文字編輯器 start-->

          <label for="summernote"> 問題正文 </label>

          <textarea name="content" id="summernote"></textarea>

          <!-- 富文字編輯器 end-->

        </div>

 

js 程式碼

$(document).ready(function() {

    $('#summernote').summernote({

        height: 300,

        tabsize: 2,

        lang: 'zh-CN',

        placeholder: ' 請輸入問題的詳細描述 ...',

        // 回撥方法在選擇圖片之後自定呼叫 onImageUpload 方法

        callbacks:{

            onImageUpload:function (files) {

                let  file = files[0];// 找到選定的圖片

                console.log(file);

                let form = new FormData();

                form.append("imageFile",file);

                $.ajax({

                    url:"/upload/image" ,

                    method:"POST",

                    data:form,

                    cache:false,

                    contentType:false,

                    processData:false,

                    success:function (r) {

                        if (r.code===OK){

                            // 上傳成功後進行載入建立 img 標籤

                            var  img= new Image();

                            img.src=r.message;

                            // 將圖片新增到 summernote

                            $("#summernote").summernote("insertNode",img);

                        }else{

                            alert(r.message);

                        }

 

                    }

                });

 

            }

        }

    });

 

});

 

這樣,一對一直播平臺原始碼就實現了圖片釋出。

宣告:本文由雲豹科技轉發自王 zming 部落格,如有侵權請聯絡作者刪除

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70002045/viewspace-2848921/,如需轉載,請註明出處,否則將追究法律責任。

相關文章