小程序上传图片到服务器,图片存储到服务器的任意磁盘

1、webservice编写接口

引用
using System.IO;
using System.Drawing;

[WebMethod(Description = "接收上传的图片")]
    public void dd_img_upload()
    {
        string txt;//提示信息
        string fileExtension;//文件扩展名
        string shijian;//当前时间
        string newname;//新文件名
        try
        {
            string path = @"E:\\A1\XXXX\";//文件路径,自定义
            HttpPostedFile file = System.Web.HttpContext.Current.Request.Files["file"];//对应小程序name
            //获取文件
            if (file != null)
            {
                Stream sr = file.InputStream;//文件流
                Bitmap bitmap=(Bitmap)Bitmap.FromStream(sr);
                //获取文件的扩展名,并转换成小写
                fileExtension = System.IO.Path.GetExtension(file.FileName).ToLower();
                //获取当前时间
                shijian = DateTime.Now.ToLocalTime().ToString("yyyyMMddhhssmm");
                //合成新产生的文件名
                newname = shijian + fileExtension;//新文件名=时间+文件扩展名
                path += newname;
                bitmap.Save( path);
            }        
            txt = "{\"errcode\":0,\"errmsg\":\"成功\"}";
        }
        catch(Exception vErr)
        {
            txt = vErr.Message;
        }
        Context.Response.Write(txt);
        Context.Response.End();
    }

2、钉钉小程序调用接口(微信小程序的调用类似)
(1)file.axml

		<view class="add_item border_bottom">
          <view class="mingcheng">附件上传<text class="red"></text></view>
          <view a:if="{{fileImagelength > 0}}" onTap="previewImage">预览图片</view>
          <view onTap="chooseImage">
            <image  src="/pages/img/add_photo.png" style="width:40px;height:40px;margin-left:5px;"/>
          </view>
        </view>

(2)file.js——目前只能上传一张图片到服务器,若想循环上传可以通过for循环

//附件上传
chooseImage() {
  var that=this
    dd.chooseImage({
      sourceType: ['camera','album'],
      count: 5,//最大可选照片数
      success: (res) => {
        that.setData({
          fileImage:res.filePaths,
          fileImagelength:res.filePaths.length,
        })
        console.log(res.filePaths[0])
        //上传图片到服务器
        dd.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示
          content: '图片上传中...',
        });
        dd.uploadFile({
          url: 'https://xxxxxxxxxx.asmx/dd_img_upload',//自己发布的webservice接口接口地址
          fileType: 'image',
          fileName: 'file',
          filePath: res.filePaths[0],//取到上传的第一张图片的图片文件
          success: res => {
            // 停止加载中动画
            dd.hideLoading();
            console.log(res);
          },
          fail: function (res) {
            // 停止加载中动画
            dd.hideLoading();
            dd.alert({ title: `上传失败:${JSON.stringify(res)}` });
          },
        });
      },
      fail:(res)=>{
        dd.alert({ 
            title: '提示',
            content: '用户取消操作'
        })
      }
    })
  },
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值