文件上传下载,图片在线预览第二种方式,element手动上传

一 文件上传

  1. 前台,使用layui或者自定义
 <button type="button" class="layui-btn" id="test1" ></button>
   var uploadInst = upload.render({
   
            elem: '#test1'
            , url: '${
   ctx}//upload'
            , accept: 'images'
            // , auto: false
            // , bindAction: '#btnUploadPicture'
            , size: 50000
            , before: function (obj) {
   
                if(applyPicPath != null && applyPicPath != ''){
   
                    this.data={
   'url': applyPicPath};
                }          
            }
            , done: function (res) {
   
                if(res.code == 0){
   
					alert("上传成功")
                }else{
   
                    return layer.msg('上传失败');
                }

            }
            , error: function () {
   
                //演示失败状态,并实现重传
                layer.alert("上传失败");
            }
        });
  1. 自己制作前台上传
 <input id="uploadPoto" style="display: none"  class ="upload-btn" type="file"  />
<div id="uploadBase" class="layui-btn" ><i class="layui-icon layui-icon-upload-drag"></i>上传</div>
  //上传
$("#uploadBase").click(function() {
   				
		$('#uploadPoto').trigger('click');					
		}
});
			  // 文件改变时 将文件名称显示到p元素中
			    $('#uploadPoto').on('change', function(event) {
   
			  
			    	if (this.files[0] == undefined || '' == this.files[0]) {
   
		                return false;
		            }
			    	var fileType = $("#fileType").val();
					var projectId = $("#project").val();
					var projectName = $("#project").find("option:selected").text();
					if(""==projectId){
   
						projectName = "";
					}
					var imagSize =  document.getElementById("uploadPoto").files[0].size;
					if(imagSize>1024*1024*50){
   
				    	alert("文件大小要求在50M以内");
				    	$("#uploadPoto").val("");
						return false;
				    }
					var loading = layer.load(0, {
   
	                    shade: true,
	                });
			        var formData = new FormData();
		 			formData.append('file',this.files[0]);  //添加图片信息的参数
			        formData.append("fileType", fileType);
			        formData.append("projectId", projectId);
			        formData.append("projectName", projectName);
			        $.ajax({
   
			            url: "${ctx}/knowledgeBase/fileManage/uploadFile",
			            type: "post",
			            dataType: "json",
			            cache: false,
			            data: formData,
			            processData: false,// 不处理数据
			            contentType: false, // 不设置内容类型
			            success: function (data) {
   
			            	layer.close(loading);
			                if(data.success == true){
   
			                	layer.alert("上传成功");
			                }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
element中,可以使用el-upload组件上传文件,并且可以配合el-dialog组件实现文件预览功能。 具体步骤如下: 1. 安装element-ui组件库 ``` npm install element-ui --save ``` 2. 在需要使用上传组件的页面引入el-upload组件和el-dialog组件 ``` import { ElUpload, ElDialog } from 'element-ui' ``` 3. 在页面中使用el-upload组件 ```html <template> <el-upload class="upload-demo" action="your-upload-api-url" :on-success="handleSuccess"> <el-button size="small" type="primary">点击上传</el-button> </el-upload> </template> <script> export default { methods: { handleSuccess(response, file, fileList) { // 处理上传成功后的回调 } } } </script> ``` 4. 在handleSuccess方法中处理上传成功后的回调,将上传成功的文件信息保存到data中 ```javascript handleSuccess(response, file, fileList) { this.fileList = fileList } ``` 5. 点击文件名,弹出el-dialog组件,显示文件预览窗口 ```html <template> <div> <el-upload class="upload-demo" action="your-upload-api-url" :on-success="handleSuccess"> <el-button size="small" type="primary">点击上传</el-button> </el-upload> <el-dialog :visible.sync="dialogVisible" width="30%"> <embed :src="previewUrl" type="application/pdf" width="100%" height="600px" /> </el-dialog> </div> </template> <script> export default { data() { return { fileList: [], dialogVisible: false, previewUrl: '' } }, methods: { handleSuccess(response, file, fileList) { this.fileList = fileList }, handlePreview(file) { // 点击文件名,打开预览窗口 this.previewUrl = URL.createObjectURL(file.raw) this.dialogVisible = true } } } </script> ``` 在以上代码中,我们使用了<embed>标签来展示PDF文件,你可以根据需要替换为其他类型的文件预览组件。同时,我们使用了URL.createObjectURL方法来生成文件的预览URL,这个方法会返回一个Blob URL,可以直接用于文件预览

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值