十九、 富文档与redis存储图片信息

文章详细描述了在Vue项目中如何处理图片和文件的显示、下载,以及与后端接口的交互。前端使用了getImgView和downloadFile方法,涉及文件访问路径处理,而表单提交时会保存图片路径。后端通过JeecgDemoController处理文件上传请求,并将数据持久化到数据库。
摘要由CSDN通过智能技术生成

列表文件

src\views\demo\jeecg\Native\one\OneNativeList.vue

<template #bodyCell="{ column, text, record }">

          <template v-if="column.dataIndex==='tupian'">

            <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>

            <img v-else :src="getImgView(text)" :preview="record.id" alt="" class="anty-img-wrap" />

          </template>

          <template v-else-if="column.dataIndex==='wenjian'">

            <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>

            <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download" size="small" @click="downloadFile(text)"> 下载 </a-button>

          </template>

其中:

getImgView(text)--à getFileAccessHttpUrl(text)

在文件src\utils\common\compUtils.ts中的如下:访问路径/sys/common/static/

export const getFileAccessHttpUrl = (fileUrl, prefix = 'http') => {

  let result = fileUrl;

  try {

    if (fileUrl && fileUrl.length > 0 && !fileUrl.startsWith(prefix)) {

      //判断是否是数组格式

      let isArray = fileUrl.indexOf('[') != -1;

      if (!isArray) {

        let prefix = `${baseApiUrl}/sys/common/static/`;

        // 判断是否已包含前缀

        if (!fileUrl.startsWith(prefix)) {

          result = `${prefix}${fileUrl}`;

        }

      }

    }

  } catch (err) {}

  return result;

};

downloadFile(text)-à getFileAccessHttpUrl()

/**

 * 文件下载

 */

function downloadFile(url) {

  if (!url) {

    createMessage.warning('未知的文件');

    return;

  }

  if (url.indexOf(',') > 0) {

    url = url.substring(0, url.indexOf(','));

  }

  url = getFileAccessHttpUrl(url.split(',')[0]);

  if (url) {

    window.open(url);

  }

}

图片文件的显示和下载的后端响应contraller与《五十一》相同同

编辑页:


src\views\demo\jeecg\Native\one\components\OneNativeModal.vue

src\views\demo\jeecg\Native\one\components\OneNativeForm.vue

<a-col :span="24">

          <a-form-item label="文件" :labelCol="labelCol" :wrapperCol="wrapperCol" v-bind="validateInfos.wenjian">

            <JUpload v-model:value="formState.wenjian"></JUpload>

          </a-form-item>

        </a-col>

        <a-col :span="24">

          <a-form-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" v-bind="validateInfos.tupian">

            <JImageUpload :fileMax="2" v-model:value="formState.tupian"></JImageUpload>

          </a-form-item>

        </a-col>

Form表单先上传图片和文件并获得图片/文件路径,再提交表单时,提交和保存图片/文件路径,富文档就保存含有图片路径的html文档。

  const url = reactive<any>({

    duplicateCheck: '/sys/duplicate/check',

    add: '/test/jeecgDemo/oneNative/add',

    edit: '/test/jeecgDemo/oneNative/edit',

  });

后端响应:

@Slf4j
@Api
(tags = "单表DEMO")
@RestController
@RequestMapping
("/test/jeecgDemo")
public class JeecgDemoController extends JeecgController<JeecgDemo, IJeecgDemoService> {
   
@Autowired
   
private IJeecgDemoService jeecgDemoService;

@PostMapping("/oneNative/add")
public Result<String> oneNativeAdd(@RequestBody JSONObject jsonObject){
   
Object oneNative = redisUtil.get("one-native");
   
JSONObject nativeObject = new JSONObject();
   
JSONArray data = new JSONArray();
   
if(null != oneNative){
       
nativeObject = (JSONObject) oneNative;
       
data = nativeObject.getJSONArray("data");
    }

//新数据id
    jsonObject.put("id", UUIDGenerator.generate());
   
data.add(jsonObject);//计入新数据
    nativeObject.put("data",data);
   
redisUtil.set("one-native",nativeObject);//存入redis数据库
    return Result.OK("添加成功");
}

  • 16
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值