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: '用户取消操作'
})
}
})
},