使用Net Core实现微信小程序上传图片(微信小程序webview上传图片)

后台环境:.net core3.1

服务器是: 华为云 Linux云服务器

小程序代码

WXML文件:

<view wx:if="{{tempFilePath}}" class="choose-pic" bindtap="openModalPic"> {{tempFilePath?'重新选择图片':'选择图片'}}</view><view wx:else class="upload-pic-btn"> <image src="{{upload_btn_pic}}" bindtap="openModalPic"></image></view><view class="pre-view-image" hidden="{{!tempFilePath}}"> <image src="{{tempFilePath}}" bindtap="preView"></image></view><view class="image-size" hidden="{{!tempFilePath||is_update_pic}}"> <view class="image-size-title"> 图片大小: </view> <view class="image-size-value "> {{size_unit_m}}M </view></view><view class=" {{max_size>=size?'image-size-tip':'image-size-error'}} " hidden="{{!tempFilePath||is_update_pic}}"> 限制上传大小1M,当前图片大小为{{size_unit_m}}M,{{max_size>=size?'大小合适':'图片过大,请重新选择!'}}</view><view class="oper-list" hidden="{{!tempFilePath}}"> <view class="oper-item" bindtap="backPage"> 返回 </view> <view class="oper-item" bindtap="uploadPic" hidden="{{is_update_pic}}"> 上传 </view></view>

JS代码文件:

wx.uploadFile API 使用

data: { tempFilePath: '', tempFiles: '', max_size: 1024 * 1024 * 1,//最大2M size: 0, size_unit_m: 0,//格式化大小 M goods_info: null, upload_btn_pic: '', base_url: '', is_update_pic: false, is_upload_temp_pic: false }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { console.log(options); console.log(getCurrentPages()); let _this = this; let index = Number(options.index); let isUploadTempPic = index === -1; _this.setData({ upload_btn_pic: operHelper.getImageDomain() '/Image/upload-pic.svg', index: index, is_upload_temp_pic: isUploadTempPic }); let pages = getCurrentPages(); let route = 'pages/BillBusiness/GoodsFiles/GoodsFiles'; if (isUploadTempPic) { route = 'pages/BillBusiness/GoodsFile/GoodsFile'; } let prePages = pages.filter(f => { return f.route === route }); let prepagesLen = prePages.length; if (prepagesLen > 0) { let tempGoods = null; let prePage = prePages[prepagesLen - 1]; if (isUploadTempPic) { tempGoods = { pic: prePage.data.pic, item_no: prePage.data.goodCode, item_name: prePage.data.goodName, }; } else { tempGoods = prePage.data.goods_list[index]; } _this.setData({ goods_info: tempGoods, offlie_url: prePage.data.upload_pic_down_url, }); if (tempGoods.pic) { _this.setData({ is_update_pic: true, tempFilePath: tempGoods.pic '&random2=' Date.now() }); } } }, openModalPic: function () { let _this = this; _this.setData({ tempFilePath: '', tempFiles: '', size: 0, size_unit_m: 0 }); wx.chooseImage({ count: 1, sizeType: ['compressed'],//只选择压缩的 sourceType: ['album', 'camera'], success: res => { console.log('success') console.log(res) if (res.errMsg === 'chooseImage:ok') { let _tempFilePaths = res.tempFilePaths; let _tempFiles = res.tempFiles; let _size = _tempFiles[0].size; _this.setData({ tempFilePath: _tempFilePaths[0], size_unit_m: (_size / 1024 / 1024).toFixed(2), size: _size, is_update_pic: false }); } }, fail: f => { console.log(f) }, complete: c => { console.log('complete') console.log(c) } }) }, preView: function () { let _this = this; let _tempFilePath = _this.data.tempFilePath; wx.previewImage({ urls: [_tempFilePath], success: r => { console.log(r) }, fail: f => { console.log(f) }, }) }, uploadPic: function () { let _this = this; let _tempFilePath = _this.data.tempFilePath; if (!_tempFilePath) { wx.showToast({ title: '请先选择图片!', icon: 'none' }) return; } if (_this.data.size > _this.data.max_size) { wx.showToast({ title: '图片内容过大,请重新选择!', icon: 'none' }) return; } let goods = _this.data.goods_info; let shopid = app.globalData.shopid ''; let uploadPath = operHelper.getImageDomain() '/' api.uploadPic; wx.showLoading({ title: '上传中...', mask: true }) wx.uploadFile({ filePath: _tempFilePath, name: 'file', url: uploadPath, formData: { "item_name": goods.item_name, "shopid": app.globalData.shopid, "sort_value": '0', "item_no": goods.item_no, "openid": app.globalData.openid, "offlie_url": _this.data.offlie_url, "is_temp": _this.data.is_upload_temp_pic ? '1' : '0', "support_image_to_product": operHelper.supportItemImageToProduct() ? "1" : "0" }, header: { "Content-Type": "multipart/form-data", "chartset": "utf-8" }, timeout: 60000, success: res => { console.log(res) console.log(JSON.parse(res.data)) let _res = JSON.parse(res.data); if (_res.success) { let extensionNameIndex = _tempFilePath.lastIndexOf('.'); let _extensionName = _tempFilePath.substr(extensionNameIndex) let pages = getCurrentPages(); let isTemp = _this.data.is_upload_temp_pic; let route = 'pages/BillBusiness/GoodsFiles/GoodsFiles'; if (isTemp) { route = 'pages/BillBusiness/GoodsFile/GoodsFile'; } let prePages = pages.filter(f => { return f.route === route }); let prepagesLen = prePages.length; if (prepagesLen > 0) { let pic = (utils.isTestEnv() ? operHelper.getImageDomain() : operHelper.getCdnImageDomain()) '/' _res.data.path '?random=' Date.now(); if (isTemp) { prePages[prepagesLen - 1].setData({ pic: pic }); } else { let tempGoodsList = prePages[prepagesLen - 1].data.goods_list; tempGoodsList[_this.data.index].pic = pic; prePages[prepagesLen - 1].setData({ goods_list: tempGoodsList }); } } wx.showModal({ content: '上传图片成功!', showCancel: false, confirmText: '确定', success: r => { wx.navigateBack() } }) } else { wx.showModal({ content: _res.message, showCancel: false, confirmText: '关闭' }) } }, fail: f => { console.log('上传失败~'); console.log(f) wx.showModal({ content: f.errMsg, showCancel: false, confirmText: '关闭' }) }, complete: c => { console.log('上传完成~'); console.log(c) wx.hideLoading({ }) } }) },

后台代码:部分关键代码

IFormFile

[HttpPost] [DontWrapResult] public async Task<Result> UploadPic(IFormFile file) { var fileName = file.FileName; string Extension = Path.GetExtension(fileName);//获取文件的源后缀 if (string.IsNullOrEmpty(Extension)) { _result.message = "文件上传的原始名称错误,没有找到文件后缀"; _result.success = false; return _result; } fileName = item_no Extension; string rootdir = AppContext.BaseDirectory; DirectoryInfo Dir = Directory.GetParent(rootdir); string root = Dir.FullName; var dissss = Environment.CurrentDirectory; string dicPath = Path.Combine(root "/wwwroot/" shopid "/" item_no); if (!Directory.Exists(dicPath)) { Directory.CreateDirectory(dicPath); } var fullName = dicPath "/" fileName; if (File.Exists(fullName)) { File.Delete(fullName); } //打开上传文件的输入流 Stream stream = file.OpenReadStream(); //开始保存拷贝文件 file.CopyTo(new FileStream(fullName, FileMode.Create)); stream.Dispose(); }

小程序页面的效果:

使用Net Core实现微信小程序上传图片(微信小程序webview上传图片)

上传页面

使用Net Core实现微信小程序上传图片(微信小程序webview上传图片)

上传后页面

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

(0)
上一篇 2024年5月9日 下午1:00
下一篇 2024年5月9日 下午1:12

相关推荐

  • 海康4200开启硬解码

    海康4200开启硬解码 随着网络技术的发展,各种视频格式的文件也开始变得越来越普遍。其中,一些视频格式的文件因为编码方式的原因,无法直接在计算机上播放,需要使用相应的解码器进行解码…

    科研百科 2024年12月7日
    27
  • 秘籍:小白从零开始玩转C语言教程,足足300集(c语言小白入门)

    引出C语言教程开篇,小编想先谈一谈,c语言的地位。 翻了翻,百度,知乎,这些编程领域,被问的最多的便是 “某某语言和某某语言哪个好,哪个有优势,哪个学完好找工作”等等。 如果当你真…

    科研百科 2023年5月20日
    99
  • 科研项目会议纪要模板(科研项目会议费一般是多少)

    科研项目会议费一般是多少 科研项目会议费是科研项目中的一个重要组成部分,用于参加学术会议、研讨会和交流活动。这些会议通常是学术研究领域的专家和学者们交流、分享研究成果和探讨学术问题…

    科研百科 2024年8月6日
    42
  • 2024年5月份党建工作计划(2024年5月份党建工作计划表)

    一、深入开展党纪学习教育。党委采取理论中心组集中学习、举办学习班等形式,逐章逐条学习《条例》;基层党组织完善学习计划,依托“三会一课”和主题党日活动,组织全体党员认真学习《条例》,…

    科研百科 2024年6月8日
    116
  • tapd项目管理系统好用吗

    tapd项目管理系统好用吗? 随着项目管理系统在现代软件开发中的普及,越来越多的团队开始使用tapd项目管理系统来提高工作效率和质量。那么,tapd项目管理系统好用吗?本文将对其进…

    科研百科 2024年12月25日
    0
  • 数智化赋能!中国铁建大桥局工程量管理系统试点项目上线启动!

    12月20日,中国铁建大桥工程局集团召开了工程量管理系统建设暨试点项目上线启动会。本次会议旨在推进集团公司工程量管理系统建设,助力项目综合管理能力提升,筑牢企业高质量发展根基,全面…

    科研百科 2024年4月25日
    125
  • 项目集成管理软件哪个好(项目集成管理软件)

    项目集成管理软件: 让项目管理更高效 随着项目的日益增多,传统的手动项目管理已经不能满足现代项目管理的需求。项目集成管理软件是一种能够帮助项目经理有效地管理项目的工具,它能够将不同…

    科研百科 2024年9月1日
    25
  • 如何从事项目管理

    如何从事项目管理 项目管理在现代商业环境中变得越来越重要。无论是在软件开发、金融、医疗保健、能源或制造业等领域,项目管理都是成功的关键因素。以下是从事项目管理的一些基本步骤和技能。…

    科研百科 2025年1月6日
    0
  • 进度计划编制软件 哪个好

    进度计划编制软件哪个好 进度计划编制软件是用于帮助组织和个人编写, 管理和发展进度计划的软件。它们通常具有自定义的模板, 工具和功能, 可帮助用户快速创建, 跟踪和分析进度计划。市…

    科研百科 2024年9月26日
    21
  • 2023年党政机关办公用房等最新标准

    2023年党政机关办公用房等最新标准 近年来,随着信息技术的不断发展和应用范围的不断扩大,党政机关办公用房的需求也越来越大。为了更好地满足这一需求,2023年党政机关办公用房等最新…

    科研百科 2024年10月28日
    6