定制小程序开发费用element-ui el-upload实现上传文件以及简单的上传文件格式验证

定制小程序开发费用在后台管理系统中总是定制小程序开发费用会用到上传文件的功能,

想实现的样式如下:(实现上传文件后,在input输入框显示文件名 )

结构代码如下:

  1. <el-form-item label="使用说明" class="uploadMain" prop="instruction">
  2. <el-input
  3. class="uploadInput"
  4. v-model="productVO.instruction"
  5. style="width: 75%"
  6. placeholder="请上传pdf格式的使用说明文件"
  7. :disabled="true"
  8. >
  9. <el-upload
  10. slot="append"
  11. class="uploadbox"
  12. ref="upload"
  13. name="file"
  14. accept=".pdf" //接受上传文件的格式此处会默认打开上传时筛选.pdf格式
  15. :show-file-list="false"
  16. :multiple="false" //如果想要一次选择多个文件 mulitiple为true
  17. action="upload"
  18. :on-change="onChange"
  19. :auto-upload="false" //自动上传如果此处为true 选择完文件就直接上传了
  20. >
  21. <!-- class="uploadbtn" -->
  22. <el-button class="uploadbtn"></el-button>
  23. </el-upload>
  24. </el-input>
  25. </el-form-item>

由于上述结构代码打开上传文件时会自动筛选accept的文件格式,但是在用户选择时仍可以自己选择全部文件,所以需要前端对上传文件进行初步的格式检验

 前端部分上传文件初步检验js代码如下:

  1. onChange(file) {
  2. // 校验格式
  3. if (['application/pdf'].indexOf(file.raw.type) == -1) {
  4. this.$message.error('请上传正确的pdf格式');
  5. return false;
  6. }
  7. this.productVO.instruction = file.name;
  8. this.productVO.instructionFile = file.raw; //上传文件时需要用到二进制,所以这里文件取值为file.raw
  9. },

上传至接口时js代码如下:

  1. submit(){
  2. const formData = new FormData();
  3. formData.append('instruction', this.productVO.instruction);
  4. formData.append('instructionFile',this.productVO.instructionFile);
  5. //调用接口
  6. this.$post('/product/create',formData,{
  7. baseURL:'/',
  8. header:{isloading: true,'Content-Type': 'multipart/form-data'}).then()
  9. }

此处涉及到文件的不同格式,列举一些常用的文件格式

 ".doc"

"application/msword"

"."

 "application/vnd.ms-excel"

"."

 "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

".xlsx"

 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

".jpeg"

 "image/jpeg"

".mp3"

"audio/x-mpeg"

".jpg"

 "image/jpeg"

".mp4"

"video/mp4"

".png"

 "image/png"

 ".pdf" "application/pdf"

".ppt"

"application/vnd.ms-powerpoint"

 ".txt"

"text/plain"

".tar"

"application/x-tar"

"."

"application/vnd.ms-works"

".zip"

 "application/x-zip-compressed"

".xml"

 "text/plain"

附加:当上传文件为多个时,具体代码如下:

  1. <el-form-item label="数据" class="uploadMain" prop="entity">
  2. <el-input
  3. class="uploadInput"
  4. v-model="productVO.entity"
  5. style="width: 75%"
  6. placeholder="请上传完整的tif/tiff/shp格式的数据文件"
  7. :disabled="true"
  8. >
  9. <el-upload
  10. slot="append"
  11. class="uploadbox"
  12. ref="upload"
  13. name="file"
  14. accept=".tif,.tiff,.shp,.dbf,.prj,.sbn,.sbx,.shx"
  15. :show-file-list="false"
  16. multiple
  17. :file-list="this.productVO.fileList"
  18. action="upload"
  19. :on-change="onChange"
  20. :auto-upload="false"
  21. >
  22. <!-- class="uploadbtn" -->
  23. <el-button class="uploadbtn"></el-button>
  24. </el-upload>
  25. </el-input>
  26. <div style="color: #ffc230">此处是文本说明</div>
  27. </el-form-item>

js代码如下:

  1. onChange(file,fileList) {
  2. // 校验格式
  3. if (['image/tiff', ''].indexOf(file4.raw.type) == -1) {
  4. this.$message.error('请上传正确的tif/tiff/shp格式');
  5. return false;
  6. }else{
  7. this.productVO.fileList=fileList
  8. console.log(this.productVO.fileList)
  9. var listName=[]
  10. this.productVO.fileList.forEach(function(e){listName.push(e.name)})
  11. var listFileRaw=[]
  12. this.productVO.fileList.forEach(function(e){listFileRaw.push(e.raw)})
  13. this.productVO.entity = listName; //文本框显示所有的文件名
  14. this.productVO.entityFile = listFileRaw;
  15. }
  16. },

接口上传文件时formData传参改动如下:

  1. this.productVO.entityFile.forEach(element => {
  2. formData.append('entityFile', element)
  3. })

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发