vant上传到服务器功能

3 篇文章 0 订阅
<van-uploader v-model="form.data"  :after-read="afterRead">
                    <div class="upImg">
                        <img style="width:2rem;height:2rem" src="../img/up.png" alt="">
                        <p>上传资料<span style="color: red;">(请上传确认资料)</span></p>
                    </div>
                </van-uploader>
            afterRead(file) {
                let _this = this;
                // 此时可以自行将文件上传至服务器
                console.log(file);
                const formData = new FormData(); // 声明一个FormData对象
                formData.append("file", file.file);
                $.ajax({
                    url: host + '/htb-admin/api/file/upLoad',
                    type: 'POST',
                    contentType: 'multipart/form-data',
                    processData: false, // 增加这一行,不处理参数
                    contentType: false,
                    data: formData,
                    success: function (res) {
                        if (res.code == 200) {
                            _this.form.data.forEach((ele, i) => {
                                if (ele.file.lastModified == file.file.lastModified) {
                                    _this.form.data[i].content = res.msg.split('htb')[1]
                                }
                            });
                        }
                    }
                })
            },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue前端开发中,可以使用Vant组件库中的Uploader组件来实现图片上传功能。同时,为了减小上传文件的大小,可以使用JavaScript中的canvas API对图片进行压缩处理。具体步骤如下: 1. 在Vue项目中引入Vant组件库和exif-js库。 2. 在Uploader组件中设置上传图片的最大尺寸和格式。 3. 在上传前,使用exif-js库获取图片的方向参数,并根据方向参数对图片进行旋转。 4. 使用canvas API对图片进行压缩处理,并将压缩后的图片上传至服务器。 代码示例: ``` <template> <van-uploader :max-size="2 * 1024 * 1024" :accept="['jpg', 'jpeg', 'png']" :before-upload="beforeUpload" :on-success="onSuccess" /> </template> <script> import EXIF from 'exif-js'; import { Toast } from 'vant'; export default { methods: { beforeUpload(file) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = (event) => { const img = new Image(); img.src = event.target.result; img.onload = () => { const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); let width = img.width; let height = img.height; let orientation = 1; EXIF.getData(file, function() { orientation = EXIF.getTag(this, 'Orientation'); }); if ([5, 6, 7, 8].indexOf(orientation) > -1) { canvas.width = height; canvas.height = width; } else { canvas.width = width; canvas.height = height; } switch (orientation) { case 2: ctx.transform(-1, 0, 0, 1, width, 0); break; case 3: ctx.transform(-1, 0, 0, -1, width, height); break; case 4: ctx.transform(1, 0, 0, -1, 0, height); break; case 5: ctx.transform(0, 1, 1, 0, 0, 0); break; case 6: ctx.transform(0, 1, -1, 0, height, 0); break; case 7: ctx.transform(0, -1, -1, 0, height, width); break; case 8: ctx.transform(0, -1, 1, 0, 0, width); break; default: break; } ctx.drawImage(img, 0, 0, width, height); const base64 = canvas.toDataURL('image/jpeg', 0.8); const blob = this.dataURLtoBlob(base64); resolve(blob); }; }; }); }, onSuccess(response) { Toast.success('上传成功'); }, dataURLtoBlob(dataURL) { const arr = dataURL.split(','); const mime = arr[0].match(/:(.*?);/)[1]; const bstr = atob(arr[1]); let n = bstr.length; const u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], { type: mime }); }, }, }; </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值