小程序上传视频及预览(含后台)

上传视频,图片等都是调用wx.uploadFile,后台接口都不用改。
.wxml

<button bindtap="chooseVideo">
  添加视频</button>

<block wx:if="{{src != ''}}">
  <video src="{{src}}"></video>
</block>

.js

  Page({
  data: {
    src: ''
  },
  //选择视频
  chooseVideo: function() {
    var that = this
    wx.chooseVideo({
      success: function(res) {
        that.setData({
          src: res.tempFilePath,
        })
        that.uploadvideo();
      }
    })
  },
  uploadvideo: function() {
    var src = this.data.src;
    wx.uploadFile({
      url: '**************/Upload', //服务器接口
      filePath: src,
      header: {
        'content-type': 'multipart/form-data'
      },
      name: 'files',
      success: function(res) {
        console.log(res.data)
      },
      fail: function() {
        console.log('接口调用失败')
      }
    })
  }
})

后台接口

  [WebMethod(EnableSession = true)]
        public void Upload()
        {
            //获取文件路径
            HttpFileCollection files = HttpContext.Current.Request.Files;
            //返回的对象初始化
            object obj = null;
            //获取项目路径
            string sPath = System.Web.HttpContext.Current.Request.PhysicalPath;
            //得到上一级路径
            DirectoryInfo di = new DirectoryInfo(string.Format(@"{0}..\..\", sPath));
            string sFilePath = Path.Combine(di.FullName, "Upload", DateTime.Today.Year.ToString(), DateTime.Today.Month.ToString(), DateTime.Today.Day.ToString(), "");
            //不存在路径则创建该路径
            if (!Directory.Exists(sFilePath))
            {
                Directory.CreateDirectory(sFilePath);
            }
            foreach (string key in files.AllKeys)
            {
                HttpPostedFile file = files[key];//file.ContentLength文件长度
                if (string.IsNullOrEmpty(file.FileName) == false)
                {
                    string fileName = Path.GetFileName(file.FileName);// 原始文件名称
                    string fileExtension = Path.GetExtension(fileName); // 文件扩展名
                    string saveName = Guid.NewGuid().ToString() + fileExtension; // 保存文件名称
                    string fileSaveAsName = string.Format(@"{0}\{1}", sFilePath, saveName);//保存文件路径
                    file.SaveAs(fileSaveAsName);//保存
                    string sUrl = Path.Combine("Upload", DateTime.Today.Year.ToString(), DateTime.Today.Month.ToString(), DateTime.Today.Day.ToString(), saveName);//Url地址
                    obj = (new { resultCode = "001", FileName = saveName, Url ="http://localhost:14146/"+sUrl });
                }
                else obj = (new { resultCode = "003", Message = "请选择要上传的文件!" });
            }
            HttpContext.Current.Response.Write(obj);
        }

结果及返回数据

上传结果
js返回数据

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值