应无所住,而生其心
排名
1
文章
860
粉丝
112
评论
163
net core webapi post传递参数
庸人 : 确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

vue elementui,vue3 element plus 文件上传的时候设置其他参数。后台.net接收传递的额外参数。图片上传

10035人阅读 2021/12/26 17:59 总访问:5182811 评论:0 收藏:0 手机
分类: 前端

比如上传文件的时候额外传递两个select选择的值

前台

前面上传文件的时候要提供默认参数很简单,el-upload绑定一个data即可

  1. <el-upload :data="data" > </el-upload>

只要data里边有数据,上传的时候会自动回传的,比如我们绑定那个select选择的值

  1. data: {
  2. filePremissonId: "",
  3. fileTypeId: ""
  4. }

对应的两个select

  1. <el-button type="success">选择文件</el-button>
  2. <el-select v-model="data.filePremissonId" style="margin-left:10px" placeholder="访问权限">
  3. <el-option v-for="item in filePremissons" :key="item.id" :label="item.filePremissonName" :value="item.id" />
  4. </el-select>
  5. <el-select v-model="data.fileTypeId" style="margin-left:10px" placeholder="文件类型">
  6. <el-option v-for="item in fileTypes" :key="item.id" :label="item.fileTypeName" :value="item.id" />
  7. </el-select>

.net后台接收

使用[FromForm]string fileTypeId这种格式接收就行

  1. public string Post(IFormCollection Files, int userId,[FromForm]string fileTypeId, [FromForm]string filePremissonId)

或者直接在request里边读取

  1. var fileTypeId = Request.Form["fileTypeId"].ToString();
  2. var filePremissonId = Request.Form["filePremissonId"].ToString();

前台完整一点的代码贴一下

  1. <template>
  2. <el-dialog v-model="dialogFormVisible" :before-close="handleClose" :close-on-click-modal="false" :title="title"
  3. width="609px">
  4. <div class="upload">
  5. <el-alert :closable="false" :title="`支持excel、word、pdf与图片格式,单次可最多选择${limit}个,每个不可大于${size}M,如果大于${size}M会自动为您过滤`"
  6. type="info" />
  7. <el-upload ref="uploadRef" accept="image/png,image/jpeg,.xls,.xlsx,.txt,.doc,.docx,.pdf,.ppt,.pptx"
  8. :action="action" :auto-upload="false" class="upload-content" :close-on-click-modal="false" :data="data"
  9. :file-list="fileList" :headers="headers" :limit="limit" list-type="text" :multiple="true" :name="name"
  10. :on-change="handleChange" :on-error="handleError" :on-exceed="handleExceed" :on-preview="handlePreview"
  11. :on-progress="handleProgress" :on-remove="handleRemove" :on-success="handleSuccess">
  12. <template #trigger>
  13. <!-- <vab-icon icon="add-line" /> -->
  14. <!-- <el-button type="primary">选择文件</el-button> -->
  15. <el-button type="success">选择文件</el-button>
  16. <el-select v-model="data.filePremissonId" style="margin-left:10px" placeholder="访问权限">
  17. <el-option v-for="item in filePremissons" :key="item.id" :label="item.filePremissonName" :value="item.id" />
  18. </el-select>
  19. <el-select v-model="data.fileTypeId" style="margin-left:10px" placeholder="文件类型">
  20. <el-option v-for="item in fileTypes" :key="item.id" :label="item.fileTypeName" :value="item.id" />
  21. </el-select>
  22. </template>
  23. <el-dialog v-model="dialogVisible" append-to-body title="查看大图">
  24. <div>
  25. <el-image :src="dialogImageUrl" />
  26. </div>
  27. </el-dialog>
  28. </el-upload>
  29. <!-- <el-select v-model="fileTypeId" @change="fileTypeChangeValue" style="margin-left:6px" placeholder="文件类型">
  30. <el-option v-for="item in fileTypes" :key="item.id" :label="item.fileTypeName" :value="item.id" />
  31. </el-select> -->
  32. </div>
  33. <template #footer>
  34. <div v-if="show" style="position: absolute; top: 10px; left: 15px; color: #999">
  35. 正在上传中... 当前上传成功数:{{ imgSuccessNum }}张 当前上传失败数:{{
  36. imgErrorNum
  37. }}张
  38. </div>
  39. <el-button type="danger" @click="handleClose">关闭</el-button>
  40. <el-button :loading="loading" style="margin-left: 10px" type="primary" @click="submitUpload">
  41. 开始上传
  42. </el-button>
  43. </template>
  44. </el-dialog>
  45. </template>
  46. <script>
  47. import { useUserStore } from '@/store/modules/user'
  48. import { getAllList as getAllFileTypeList } from '@/api/fileTypeManager'
  49. import { ElLoading } from 'element-plus'
  50. import _ from 'lodash'
  51. export default defineComponent({
  52. name: 'VabUploadFile',
  53. props: {
  54. url: {
  55. type: String,
  56. default: 'http://localhost:8002/upload222',
  57. required: true,
  58. },
  59. name: {
  60. type: String,
  61. default: 'file',
  62. required: true,
  63. },
  64. limit: {
  65. type: Number,
  66. default: 20,
  67. required: true,
  68. },
  69. size: {
  70. type: Number,
  71. default: 20,
  72. required: true,
  73. },
  74. },
  75. setup(props, { emit }) {
  76. const userStore = useUserStore()
  77. const { token } = storeToRefs(userStore)
  78. const $baseMessage = inject('$baseMessage')
  79. let fullscreenLoading = null;
  80. const state = reactive({
  81. uploadRef: null,
  82. show: false,
  83. loading: false,
  84. dialogVisible: false,
  85. dialogImageUrl: '',
  86. action: '',
  87. headers: {},
  88. fileList: [],
  89. limit: 10,
  90. size: 10,
  91. picture: 'picture',
  92. imgNum: 0,
  93. allImgNum: 0,
  94. imgSuccessNum: 0,
  95. imgErrorNum: 0,
  96. typeList: null,
  97. title: '上传',
  98. dialogFormVisible: false,
  99. fileTypes: [
  100. {
  101. id: '1',
  102. fileTypeName: 'Option1',
  103. },
  104. {
  105. id: '2',
  106. fileTypeName: 'Option2',
  107. },
  108. {
  109. id: '3',
  110. fileTypeName: 'Option3',
  111. }
  112. ],
  113. filePremissons: [
  114. {
  115. id: '1',
  116. filePremissonName: '同部门可见',
  117. },
  118. {
  119. id: '2',
  120. filePremissonName: '仅自己可见',
  121. },
  122. {
  123. id: '3',
  124. filePremissonName: '所有人可见',
  125. }
  126. ],
  127. //上传时候可以附带的额外的参数。比如绑定select值的时候可以用里边的方便直接回传
  128. data: {
  129. filePremissonId: "",
  130. fileTypeId: ""
  131. },
  132. })
  133. const submitUpload = () => {
  134. if (state.allImgNum <= 0) {
  135. $baseMessage(
  136. `请选择需要上传的文件,超过10M的文件将无法上传`,
  137. 'warning',
  138. 'vab-hey-message-warning'
  139. )
  140. return
  141. }
  142. fullscreenLoading = ElLoading.service({
  143. lock: true,
  144. text: '文件上传,转换中...',
  145. background: 'rgba(255, 255, 255, 0.6)',
  146. })
  147. state.uploadRef.submit()
  148. }
  149. const handleProgress = () => {
  150. state.loading = true
  151. state.show = true
  152. }
  153. const handleChange = (file, fileList) => {
  154. if (fileList && fileList.length > 0) {
  155. if (file.size > 1048576 * state.size) {
  156. fileList.filter((item) => item !== file)
  157. state.fileList = fileList
  158. $baseMessage(
  159. `文件超过10M了`,
  160. 'warning',
  161. 'vab-hey-message-warning'
  162. )
  163. } else {
  164. state.allImgNum = fileList.length
  165. }
  166. }
  167. }
  168. const handleSuccess = (response, file, fileList) => {
  169. state.imgNum = state.imgNum + 1
  170. state.imgSuccessNum = state.imgSuccessNum + 1
  171. if (fileList.length === state.imgNum) {
  172. setTimeout(() => {
  173. if (fullscreenLoading != null) {
  174. fullscreenLoading.close();
  175. }
  176. $baseMessage(
  177. `上传完成! 共上传文件${fileList.length}个`,
  178. 'success',
  179. 'vab-hey-message-success'
  180. )
  181. }, 1000)
  182. }
  183. setTimeout(() => {
  184. state.loading = false
  185. state.show = false
  186. }, 1000)
  187. }
  188. const handleError = (err, file) => {
  189. state.imgNum = state.imgNum + 1
  190. state.imgErrorNum = state.imgErrorNum + 1
  191. $baseMessage(
  192. `文件[${file.raw.name}]上传失败,文件大小为${_.round(
  193. file.raw.size / 1024,
  194. 0
  195. )}KB`,
  196. 'error',
  197. 'vab-hey-message-error'
  198. )
  199. if (fullscreenLoading != null) {
  200. fullscreenLoading.close();
  201. }
  202. setTimeout(() => {
  203. state.loading = false
  204. state.show = false
  205. }, 1000)
  206. }
  207. const handleRemove = () => {
  208. state.imgNum = state.imgNum - 1
  209. state.allNum = state.allNum - 1
  210. }
  211. const handlePreview = (file) => {
  212. //alert('选择了文件,点击才会出现的')
  213. state.dialogImageUrl = file.url
  214. state.dialogVisible = true
  215. }
  216. const handleExceed = (files) => {
  217. $baseMessage(
  218. `当前限制选择 ${state.limit} 个文件,本次选择了
  219. ${files.length}
  220. 个文件`,
  221. 'error',
  222. 'vab-hey-message-error'
  223. )
  224. }
  225. const handleShow = (data) => {
  226. state.title = '上传'
  227. // console.log(data);
  228. // console.log("------------------------------------------------------------------------");
  229. // 这里data没有值的时候不要乱赋值,不然赋值成了undefined就有问题了
  230. // state.data = data
  231. state.dialogFormVisible = true
  232. }
  233. //点击关闭文件
  234. const handleClose = () => {
  235. state.fileList = []
  236. state.picture = 'picture'
  237. state.allImgNum = 0
  238. state.imgNum = 0
  239. state.imgSuccessNum = 0
  240. state.imgErrorNum = 0
  241. state.headers['Authorization'] = `Bearer ${token.value}`
  242. state.dialogFormVisible = false
  243. //重写执行一次调用这个组件的页面的查询方法,刷新数据
  244. emit('fetch-data')
  245. }
  246. //获取文件类型
  247. const fetchAllFileType = async () => {
  248. const {
  249. data: { list },
  250. } = await getAllFileTypeList(state.queryForm)
  251. state.fileTypes = list
  252. }
  253. onMounted(() => {
  254. fetchAllFileType()
  255. console.log(token)
  256. console.log(token.value)
  257. //给token 原来的写法,token是一个对象,传回去传不到token,就验证不通过
  258. //state.headers['Authorization'] = `Bearer ${token}`
  259. //要点里边的value才是里边的token
  260. state.headers['Authorization'] = `Bearer ${token.value}`
  261. console.log(props.url)
  262. //这里给上传地址
  263. state.action = props.url
  264. })
  265. const percentage = computed(() => {
  266. if (state.allImgNum === 0) return 0
  267. return _.round(state.imgNum / state.allImgNum, 2) * 100
  268. })
  269. return {
  270. ...toRefs(state),
  271. submitUpload,
  272. handleProgress,
  273. handleChange,
  274. handleSuccess,
  275. handleError,
  276. handleRemove,
  277. handlePreview,
  278. fetchAllFileType,
  279. handleExceed,
  280. handleShow,
  281. handleClose,
  282. percentage,
  283. }
  284. },
  285. })
  286. </script>
  287. <style lang="scss" scoped>
  288. .upload {
  289. height: 366px;
  290. .upload-content {
  291. .el-upload__tip {
  292. display: block;
  293. height: 30px;
  294. line-height: 30px;
  295. }
  296. :deep() {
  297. .el-upload--picture-card {
  298. width: 128px;
  299. height: 128px;
  300. margin: 3px 8px 8px 8px;
  301. border: 2px dashed #c0ccda;
  302. .ri-add-line {
  303. font-size: 24px;
  304. }
  305. }
  306. .el-upload-list--picture {
  307. margin-bottom: 20px;
  308. }
  309. .el-upload-list--picture-card {
  310. .el-upload-list__item {
  311. width: 128px;
  312. height: 128px;
  313. margin: 3px 8px 8px 8px;
  314. }
  315. }
  316. }
  317. }
  318. }
  319. </style>

欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价

Quartz.net实例动态改变周期调度misfire、Cron

Quartz:Java编写的开源的任务调度作业框架 类似Timer之类定时执行的功能,但是更强大Quartz.NET:是把Quartz转成C# NuGet...

.net Windows服务发布、安装、卸载、监听脚本服务调试

一、脚本 为方便不用每次都去写安装卸载的脚本1.安装脚本@echooff @echo开始安装【服务】 %SystemRoot%\Microsoft.NET\Fr...

nginx常用命令nginx启动命令nginx重启命令nginx关闭命令nginx测试配置文件是否正确nginx nginx.pid文件丢失报错

启动命令:start nginx 关闭命令:nginx -s stop nginx -s quit nginx -s stop与nginx -s quit区别 Quit is a graceful shu...

DevExpress.XtraSpreadsheet.SpreadsheetControl控件 加载excel模板

stringpath=&quot;文件路径&quot;; DevExpress.XtraSpreadsheet.SpreadsheetControlspreadsheetControl=newDevExpress.Xtr...

上传文件到服务器及 下载到 客户端

usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Net; usingSystem.IO; namespaceCo...

使用OutLook发送邮件

publicstaticvoidOutlook(stringSubject,stringTextBody,stringFromAdd,stringFromPass,stringTo,stringCC,List&lt;string&...

类型“DbSet”在未引用的程序集中定义必须添加对程序集“EntityFramework Version=5.0.0.0 Culture=neutral PublicKeyToken=b7

在用mvc+ef的时候在DAL层引用上下文信息的时候会报出下面错误其实就是没得EntityFromwork,打开vs项目,点击工具,选择NuGe...

SQL Server 中使用游标

--声明一个游标 DECLAREMyCursorCURSOR FORSELECTTOP5FBookName,FBookCodingFROMTBookInfo//定义一个叫MyCursor的游标,...

C委托与事件

1.什么是委托?  委托在C#里的意义和在现实里差不多,从字面意思理解即可。举个例子:领导委托小张去传递个文件,这就是...

正则表达式匹配中文标点符号

//匹配这些中文标点符号。?!,、;:“”‘&#39;()《》〈〉【】『』「」﹃﹄〔〕…—~﹏¥ varreg=/[\u3002|\uff1f|\...

泛型简单介绍

说到了泛型,就介绍下泛型泛型不是特指具体类型,是一种可变类型,可以把他看做一个类型占位符,根据传入的类型 延迟声明具...

数据读取器与指定的"xx"不兼容某个类型为"xx"的成员在同名的数据读取器中没有对应的列

报错的地方var result= _db.Database.SqlQuery&lt;SMachine&gt;(sql).FirstOrDefault();经过分析,是因为SqlQuery方法查询...

远程服务器返回错误: (403) 已禁止

今天调用接口的时候报的错。我们只要加上这两句代码就行了呢HttpWebRequestreq=(HttpWebRequest)HttpWebRequest.Create(url...

NPOI读取excelexcel 导入日期类型读取

NPOI是一个优秀的操作excel的库,可以很方便的进行excel的读取与导出NPOI读取excelpublicActionResultReadExcel() { //打...

NPOI导出excel根据模板导出Excel

使用NPOI导出excel///&lt;summary&gt; ///导出excel(下载excel) ///&lt;/summary&gt; publicvoidToExcel() { HSSFWo...