第12讲创建图文投票实现

创建图文投票实现

图文投票和文字投票基本一样,就是在投票选项里面,多了一个选项图片;

在这里插入图片描述

<view class="option_item" v-for="(item,index) in options" :key="item.id">
					<view class="option_input">
						<text class="removeOption" @click="removeOption(item.id)">&#xe618;</text><input type="text" v-model="item.name" placeholder="输入选项名称" placeholder-style="color:#bababa;font-size:14px">
					</view>				
					<view class="option_upload">
						<uni-file-picker
						@select="selectVoteItemFileFunc($event,index)"
						:auto-upload="false" 
						limit="1"
						:del-icon="false" 
						disable-preview 
						file-mediatype="image" 
						:imageStyles="voteItemImageStyles">
							<view class="upload">
								<text class="smallUploadImg">&#xe727;</text>
							</view>
						</uni-file-picker>
					</view>
				</view>
.option_item{
   
					margin-top: 10px;
					border-radius: 5px;
					background-color: white;
					padding: 10px;
					.option_input{
   
						display: flex;
					}
					.option_upload{
   
						margin-top: 20rpx;
						.upload{
   
							margin: 10rpx;
							background-color: #f4f5f7;
							width:90rpx;
							height: 90rpx;
							display: flex;
							align-items: center;
							justify-content: center;
						}
					}
				}
			voteItemImageStyles:{
   
				width:"150rpx",
				height:"120rpx",
				border:false
			},
selectVoteItemFileFunc:function(e,index){
   
				console.log("index="+index)
				console.log(e.tempFilePaths[0])
				uni.uploadFile({
   
					header:{
   token:uni.getStorageSync("token")},
					url:getBaseUrl()+"/vote/uploadVoteItemImage",
					filePath:e.tempFilePaths[0],
					name:"voteItemImage",
					success: (res) => {
   
						let result=JSON.parse(res.data);
						if(result.code==0){
   
							this.options[index].image=result.voteItemImageFileName;
						}
					}
				})
			},

加个image属性

在这里插入图片描述
提交加上验证:

// 验证投票选项,如果有名称的,必须要上传图片
				for(var i=0;i<this.options.length;i++){
   
					var option=this.options[i];
					if(!isEmpty(option.name)){
   
						if(isEmpty(option.image)){
   
							console.log("请上传第"+(i+1)+"个投票选项图片")
							uni.showToast({
   
								icon:"error",
								title:"请上传第"+(i+1)+"个投票选项图片"
							})
							return;
						}
					}
				}

在这里插入图片描述
后端:

voteItemImagesFilePath: D://uniapp/voteItemImgs/
@Value("${voteItemImagesFilePath}")
private String voteItemImagesFilePath;
/**
 * 上传投票选项图片
 * @param voteItemImage
 * @return
 * @throws Exception
 */
@RequestMapping("/uploadVoteItemImage")
public Map<String,Object> uploadVoteItemImage(MultipartFile voteItemImage)throws Exception{
   
    System.out.println("filename:"+voteItemImage.getName());
    Map<String,Object> resultMap=new HashMap<>();
    if(!voteItemImage.isEmpty()){
   
        // 获取文件名
        String originalFilename = voteItemImage.getOriginalFilename();
        String suffixName=originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName= DateUtil.getCurrentDateStr()+suffixName;
        FileUtils.copyInputStreamToFile(voteItemImage.getInputStream(),new File(voteItemImagesFilePath+newFileName));
        resultMap.put("code",0);
        resultMap.put("msg","上传成功");
        resultMap.put("voteItemImageFileName",newFileName);
    }
    return resultMap;
}

相关推荐

  1. 10投票创建页面实现

    2024-02-16 03:10:02       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-16 03:10:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-16 03:10:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-16 03:10:02       82 阅读
  4. Python语言-面向对象

    2024-02-16 03:10:02       91 阅读

热门阅读

  1. 「优选算法刷题」:除自身以外数组的乘积

    2024-02-16 03:10:02       61 阅读
  2. 算法刷题day12

    2024-02-16 03:10:02       58 阅读
  3. MySQL学习Day15——MySQL架构

    2024-02-16 03:10:02       49 阅读
  4. 奇异递归模板模式应用2-单例模板

    2024-02-16 03:10:02       58 阅读
  5. 2024/2/14

    2024-02-16 03:10:02       53 阅读
  6. Introduction to GraphQL-style APIs

    2024-02-16 03:10:02       46 阅读
  7. 在STM32微控制器中实现高速数据传输的DMA技巧

    2024-02-16 03:10:02       65 阅读
  8. 学习总结11

    2024-02-16 03:10:02       47 阅读
  9. 【30秒看懂大数据】数据标准

    2024-02-16 03:10:02       57 阅读