ios11.3以上不能使用ajax提交图文的formdata问题困扰了我好久,但是针对网页开发没有什么基础,比较习惯狐表的前端
所以想办法使用微信上传图片的方法,获取图片的素材id,然后服务端下载素材保存到指定地方。
显示的时候,关闭uploader的上传功能
虽然交互上有点区别,但是还是能用的,有需要的可以看看
主页面代码:
Http_File_Do_DTL_wx
Dim e As RequestEventArgs =
Args(0)
Dim openid As String
Dim entid As String
Dim workitemid As String
Dim Dtlid As String
Dim ss As String
Dim wb As New WeUI
Dim xdr As DataRow
Dim entname As String
Dim sdate As
String=format(Date.Today,"yyyyMMdd")
Dim fxlx As String ="完成|未完成"
Dim gov As String
If
e.host=Vars("V_host") Then
gov="gov"
End If
If e.GetValues.ContainsKey("gov") Then
gov=e.GetValues("gov")
End If
If e.GetValues.ContainsKey("itemid") Then
workitemid=e.GetValues("itemid")
End If
If e.GetValues.ContainsKey("fileid")
Then
ss =e.GetValues("fileid")
End If
If e.GetValues.ContainsKey("delwid")
Then '删除操作
Dim r1 As
DataRow=DataTables("C_PYH_WORK_Process").SQLFind("
dtl_id='" & e.GetValues("delwid") & "' ")
If r1 IsNot Nothing
r1("IS_ACTIVE")="0"
r1("UPDATE_TIME")=now.Tostring.replace("-","").replace("
","").replace(":","")
r1.save()
workitemid=r1("WORK_id")
End If
'删除成功提示
With
wb.AddMsgPage("","msgpage","删除成功", "认真 仔细 务实!")
'生成成功提示页
With
wb.AddButtonGroup("","btg1",False) '垂直排列
.Add("btn1","返回","","/entFilesInfo.htm?fileid="
& workitemid )
.Add("btn3", "撤销删除","",
"/Filedodtl.htm?recwid=" &
e.GetValues("delwid") ).Kind = 1
End With
End With
e.WriteString(wb.Build)
Return ""
End If
If e.GetValues.ContainsKey("recwid")
Then '恢复删除
Dim r1 As
DataRow=DataTables("C_PYH_WORK_Process").SQLFind("
dtl_id='" & e.GetValues("recwid") & "' ")
If r1 IsNot Nothing
r1("IS_ACTIVE")="1"
r1.save()
workitemid=r1("WORK_id")
End If
'删除成功提示
With
wb.AddMsgPage("","msgpage","撤销删除成功", "认真 仔细 务实!")
'生成成功提示页
.AddButton("btn1","返回","/entFilesInfo.htm?fileid="
& workitemid)
End With
e.WriteString(wb.Build)
Return ""
End If
Dim fh As
String="False"
Dim fdd As DataRow
Dim fdp As DataRow
If gov="gov" Then
If
e.Cookies.ContainsKey("openid") Then '从cookie中获取微信用户id
' Openid =
e.Cookies("openid")
Openid =
Functions.Execute("CheckOpenid",e.Cookies("openid"))
End If
Else
If
e.Cookies.ContainsKey("pxopenid") Then '从cookie中获取微信用户id
Openid =
e.Cookies("pxopenid")
End If
Dim fdx As DataRow=
DataTables("S_PYH_WCHACT_INFO").SQLFind("WCHAT_ID= '" &
Openid & "' and IS_ACTIVE='1' ")
If fdx IsNot Nothing Then
entid = fdx("enterprise_id")
Dim xd As
DataRow=DataTables("C_EI_ENTERPRISEINFO").SQLFind("enterprise_id='"&
entid &"' and is_active='1' ")
If xd IsNot Nothing Then
entname=xd("enterprise_name")
End If
Else
With
wb.AddMsgPage("","msgpage","缺少必要信息,无法访问", "认真
仔细 务实!") '生成成功提示页
.icon = "Warn" '改变图标
.AddButton("btn1","返回","/entFilesInfo.htm?fileid="
& SS )
End With
e.WriteString(wb.Build)
Return ""
End If
End If
If e.PostValues.Count = 0 Then '
评估页面首次打开
wb.AddPageTitle("","ph1","落实反馈记录")
wb.AppendHTML("<script
src='https://res.wx.qq.com/open/js/jweixin-1.2.0.js'></script>",True)
'引入JS-SDK库
wb.AppendHTML("<script
src='./lib/ajaxform_wxfk.js'></script>") '引入脚本文件
wb.AppendHTML("<link
rel='stylesheet' href='weui/weui.me.css'>")
'在页面注入权限验证配置
Dim st As New Date(1970,1,1,8,0,0)
Dim appid As String =
Vars("VCorpID_mp") 'CorpID
Dim timestamp As Integer = CInt((Date.Now -
st).TotalSeconds()) '时间戳
Dim noncestr As String =
Rand.NextString(16) '随机字符
Dim url As String = e.Request.URL.ToString '当前页面地址
'Dim r As Row=Tables("表B").AddNew()
'r("第一列")=url
Dim signature As String =
Functions.Execute("GetJsSignature_mp", noncestr, timestamp, url) '生成权限验证签名
Dim cfg As String =
"wx.config({appId:'{0}',timestamp:{1},nonceStr:'{2}',signature:'{3}',jsApiList:['checkJsApi','chooseImage','previewImage','uploadImage','downloadImage','getNetworkType','openLocation','getLocation','scanQRCode']});"
wb.AppendHTML("<script>"
& CExp(cfg,appid,timestamp,noncestr,signature) &
"</script>",True)
'开始正常生成网页内容
wb.AddToast("","tst1",
"正在上传",1)
wb.AddToast("","tst2",
"反馈成功",0)
wb.AddToast("","tst3",
"反馈失败",0).Icon= "warn"
wb.AddTopTips("","toptip1","请选择完成状态,输入情况描述!")
wb.AddForm("","form1","wxUpload.htm")
With
wb.AddDialog("","dlg1", "反馈成功","") '增加订单失败提示框
' .AddButton("btnOK","确定","/entFilesInfo.htm?fileid="
+ ss )
If gov="gov" Then
.AddButton("btnOK","确定","/Filedodtl.htm?itemid="
+ workitemid )
Else
.AddButton("btnOK","确定","/entPIindex.htm")
End If
End With
With
wb.AddDialog("","dlg3", "错误","") '增加订单失败提示框
.AddButton("btnOK","确定")
End With
With
wb.AddInputGroup("form1","post","")
.AddHiddenValue("post",Functions.Execute("POSTEncrypt"))
End With
If workitemid >"" Then
'校验是否历史记录,若是历史记录
xdr =
DataTables("C_PYH_WORK_Process").SQLFind("dtl_id= '" & workitemid & "' and
is_active='1' ")
With
wb.AddInputGroup("form1","ipg1","报送单位:" +
xdr("enterprise_name"))
.AddHiddenValue("work_id",ss)
.AddHiddenValue("workitemid",workitemid
)
.AddHiddenValue("serverid","")
With
.AddUploader("up1","照片资料",True) 'True表示允许上传多个文件
.AllowAdd = False
Dim sdr As DataRow =
DataTables("C_PYH_WORK_Proc_FILES").SQLFind("process_id =
'" & xdr("dtl_id")
& "' and IS_ACTIVE ='1' ")
If sdr IsNot Nothing Then
For Each dr As DataRow In
DataTables("C_PYH_WORK_Proc_FILES").SQLSelect("process_id =
'" & xdr("dtl_id")
& "' and is_active='1' ")
If
dr("FILES_SAVENAME_S") <>"" Then
.AddImage( dr("FILES_SAVENAME_S"), dr("FILES_SAVENAME"))
Else
.AddImage( dr("FILES_SAVENAME") )
End If
Next
End If
End With
' Dim fjlx As String
=fxlx.replace(xdr("WORK_STATUS"),"["&
xdr("WORK_STATUS") & "]")
End With
wb.InsertHTML("","<div id='divlist'>")
wb.InsertHTML("","</div>") '占位的DIV标签的结束标签
wb.InsertHTML("","<div id='divlist2'>")
wb.InsertHTML("","</div>") '占位的DIV标签的结束标签
With
wb.AddButtonGroup("form1","btup",False)
.Add("chooseimage",
"选择图片", "button").Kind = 1
'.Add("previewimage",
"上传图片", "button").Kind = 1
End With
With
wb.AddInputGroup("form1","ipg2","情况描述(上报时间:" +
xdr("publish_date") +")")
If
xdr("WORK_STATUS")="完成" Then
.AddSelect("WORK_STATUS","完成状态","[完成]|未完成"
)
Else
.AddSelect("WORK_STATUS","完成状态","完成|[未完成]"
)
End If
Dim pname As
String=Functions.Execute("getPNameByOpenid",xdr("creator"))
If pname>"" Then
.AddInput("xm","上报人","text").value=pname
End If
.AddTextArea("WORK_CONTENT").Value =
xdr("WORK_CONTENT")
End With
With
wb.AddButtonGroup("form1","btg1",False)
.Add("btn1", "更新上报",
"button").Attribute= "" '调用js函数上传
If
xdr("creator")=openid Then
.Add("btn2","删除","","/Filedodtl.htm?delwid="
& workitemid ).Kind = 2
End If
.Add("btn3","返回").Attribute
= ""
End With
Else
'空白记录显示
With
wb.AddInputGroup("form1","ipg1","")
With
.AddUploader("up1","照片资料",True) 'True表示允许上传多个文件
.AllowAdd = False
End With
End With
wb.InsertHTML("","<div id='divlist'>")
wb.InsertHTML("","</div>") '占位的DIV标签的结束标签
wb.InsertHTML("","<div id='divlist2'>")
wb.InsertHTML("","</div>")
'占位的DIV标签的结束标签
With
wb.AddButtonGroup("form1","btup",False)
.Add("chooseimage",
"选择图片", "button").Kind = 1
'.Add("previewimage",
"上传图片", "button").Kind = 1
End With
With wb.AddInputGroup("form1","ipg2","情况描述")
.AddSelect("WORK_STATUS","完成状态","|"+ fxlx)
.AddHiddenValue("work_xx","kong")
.AddHiddenValue("work_id",ss)
.AddHiddenValue("workitemid",workitemid )
.AddHiddenValue("serverid","")
.AddTextArea("WORK_CONTENT").Placeholder = "请输入200字以内的进展情况描述,可简短描述"
End With
End If
End if
e.WriteString(wb.Build)
页面js代码:
wx.ready(function () {
// 5.1 拍照、本地选图
var images = {
localId: [],
serverId: []
};
document.getElementById('chooseimage').onclick = function () {
wx.chooseImage({
//count: 2, // 默认9
success: function (res) {
images.localId = res.localIds;
//alert('已选择 ' + res.localIds.length + ' 张图片');
var i = 0, length = images.localId.length;
images.serverId = [];
function upload() {
wx.uploadImage({
localId: images.localId[i],
success: function (res) {
i++;
//alert('已上传:' + i + '/' + length);
images.serverId.push(res.serverId);
if (i < length) {
upload();
}
document.getElementById("serverid").value = res.serverId + "</p>" + document.getElementById("serverid").value;
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
}
upload();
document.getElementById("divlist").innerHTML ='已选择 ' + res.localIds.length + ' 张图片';
}
});
};
});
wx.error(function (res) {
//alert(res.errMsg);
});
function submitForm(){
var v0 = document.getElementById("work_id").value;
var v4 = document.getElementById("workitemid").value;
var v1 = document.getElementById("work_content").value;
var v2 = document.getElementById("work_status").value;
var v3 = document.getElementById("serverid").value;
if (v3){
}
else
{
if(v4){
}
else{
alert("请先选择图片再上传!");
return;
}
}
if (v1 && v2 ){
show("tst1");
var result = sendAjaxText(v0+"|"+v4+"|"+v1+"|"+v2+"|"+v3,"wxUpload.htm","afterSubmit",true);
}
else
{
show("toptip1",2000);
}
}
function afterSubmit(result){
hide("tst1");
if (result=='OK') {
showDialog('dlg1','上报成功','你的反馈已接收,请在上报记录查看上报情况!')
}
else{
showDialog('dlg3','错误',result)
}
}
ajax字符串接收网页:
Http_File_Upload_wx (wxUpload.htm)
Dim e As RequestEventArgs =
Args(0)
Dim str As String = e.PlainText
Dim wb As new WeUI
Dim openid As String
Dim entid As String
Dim entname As String
Dim sdate As
String=format(Date.Today,"yyyyMMdd")
If
e.Cookies.ContainsKey("pxopenid") Then '从cookie中获取微信用户id
Openid = e.Cookies("pxopenid")
End If
Dim fdx As DataRow=
DataTables("S_PYH_WCHACT_INFO").SQLFind("WCHAT_ID= '" &
Openid & "' and IS_ACTIVE='1' ")
If fdx IsNot Nothing Then
entid = fdx("enterprise_id")
Dim xd As
DataRow=DataTables("C_EI_ENTERPRISEINFO").SQLFind("enterprise_id='"&
entid &"' and is_active='1' ")
If xd IsNot Nothing Then
entname=xd("enterprise_name")
End If
Else
With
wb.AddMsgPage("","msgpage","缺少必要信息,无法访问", "认真
仔细 务实!") '生成成功提示页
.icon = "Warn" '改变图标
.AddButton("btn1","返回","/entPIindex.htm")
End With
e.WriteString(wb.Build)
Return ""
End If
Dim ss As String
Dim workitemid As String
Dim content As String
Dim status As String
Dim mediaids As String
Dim Values() As String
Values = str.split("|")
If values.length=5 Then
ss=Values(0)
workitemid =Values(1)
content=Values(2)
status=Values(3)
mediaids=Values(4)
End If
Dim dr As DataRow
If workitemid >""
Then '判断是否已有记录
dr =
DataTables("C_PYH_WORK_Process").SQLFind("dtl_id= '" & workitemid & "' and
is_active='1' ")
Else '否则新增记录
Dim mdr As DataRow
=DataTables("C_PYH_WORK_Process").SQLFind("WORK_id ='"&
ss &"' and IS_ACTIVE='1' and WORK_STATUS ='"& status
&"' And dept_id='"& entid &"'
")
If mdr IsNot Nothing Then
dr = mdr
workitemid=mdr("dtl_id")
Else
dr=DataTables("C_PYH_WORK_Process").SQLAddNew()
workitemid =Guid.NewGuid().ToString().replace("-","")
dr("work_id")=ss
dr("CREATE_TIME")=now.Tostring.replace("-","").replace("
","").replace(":","")
dr("dtl_id")=workitemid
dr("IS_ACTIVE")="1"
dr("publish_date")=sdate
If openid>"" Then
dr("creator")=openid
dr("dept_id")=entid
dr("enterprise_name")=entname
Else
dr("creator")="test"
dr("dept_id")="2DB0F74160C94EBD8CD7BB3F186ED4D1"
End If
End
If
End If
dr("WORK_STATUS")=status
dr("WORK_CONTENT")=content
'保存并生成增加成功提示页面
dr("UPDATE_TIME")=now.Tostring.replace("-","").replace("
","").replace(":","")
dr.save()
Dim Pics() As
String=mediaids.replace("</p>","|").split("|")
For i As Integer =0 To
pics.length-1
If pics(i)>"" Then
Dim s As
String=Functions.Execute("Http_WX_Pic_save",pics(i),"process")
Dim dr2 As DataRow =
DataTables("C_PYH_WORK_Proc_FILES").SQLAddNew()
If s <>"失败" And
s<> "" Then
Dim s0 As String= s & ".jpg" '保存的文件名
Dim s1 As String= s &
"-s.jpg" '保存的文件名
dr2("FILES_SAVENAME") =
s0
dr2("FILES_SAVENAME_S") =
s1
Else
Functions.Execute("ErrLogText",pics(i) + "素材获取失败!")
End If
dr2("FILES_NAME") = pics(i)
dr2("file_id")=Guid.NewGuid().ToString().replace("-","")
dr2("process_id")=workitemid
dr2("CREATE_TIME")=now.Tostring.replace("-","").replace("
","").replace(":","")
dr2("UPDATE_TIME")=now.Tostring.replace("-","").replace("
","").replace(":","")
dr2("IS_ACTIVE")="1"
If openid>"" Then
dr2("creator")=openid
Else
dr2("creator")="test"
End If
dr2.save()
End If
Next
e.WriteString("OK")
微信上传素材下载:
Http_WX_Pic_save
Dim mediaID As String=args(0)
Dim ps As String =args(1) 'process
Dim y As String
=Date.Today.year.Tostring
Dim m As
String=Date.Today.Month.Tostring
Dim s As String=Guid.NewGuid().ToString().replace("-","")
Dim s0 As String= s & ".jpg" '保存的文件名
Dim s1 As String= s &
"-s.jpg" '保存的文件名
Dim fl As String = ProjectPath
& "web\upload\" & ps & "\" & y &
"\" & m & "\"
Dim sucess As Boolean = True '用于标记是否下载成功
Dim url As String = "https://api.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}"
url = CExp(url,
Functions.Execute("GetAccessToken"), mediaID)
Dim hc As New HttpClient(url)
try
If hc.GetFile(fl & s0) Then
If
hc.ResponseContentType.StartsWith("text/plain") Then
Dim jo As JObject =
JObject.Parse(Filesys.ReadAllText(fl))
Functions.Execute("ErrLogText","素材下载失败1:" +
jo.ToString)
FileSys.DeleteFile(fl) '删除文件
sucess = False
End If
Else
sucess = False
End
If
Catch ex As Exception
Vars("gettoken") =True
Functions.Execute("ErrLogText","素材下载失败2:" +
ex.Message )
url = CExp(url,
Functions.Execute("GetAccessToken"), mediaID)
Dim hc2 As New HttpClient(url)
hc2.GetFile(fl & s0)
End Try
If sucess Then
Dim img As image = getImage(fl & s0)
Dim bmp As bitmap = new bitmap(img , 50,
50)
bmp.save(fl & s1)
Functions.Execute("txtProgress","临时素材下载成功!")
Return "./upload/" & ps &
"/" & y & "/" & m & "/" & s
Else
Return "失败"
End If
此主题相关图片如下:qq截图20180718143352.png
此主题相关图片如下:微信图片_20180718152116.jpg