Foxtable(狐表)用户栏目专家坐堂 → [求助]选择图片后直接上传如何写代码


  共有3184人关注过本帖树形打印复制链接

主题:[求助]选择图片后直接上传如何写代码

帅哥哟,离线,有人找我吗?
ajie5211
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:四尾狐 帖子:820 积分:5641 威望:0 精华:1 注册:2015/11/28 14:00:00
[求助]选择图片后直接上传如何写代码  发帖心情 Post By:2018/9/14 9:27:00 [只看该作者]

输入页面代码如下

Dim e As RequestEventArgs = args(0)
Dim wb As New weui
    wb.AddForm("","form1","cardbookaddnew.htm")
    With wb.AddInputGroup("form1","ipg1","名片正面") '文件上传
        .AddUploader("名片正面","",False)
        .Attribute = "" '调用js函数
    End With
    With wb.AddInputGroup("form1","ipg2","名片信息")
        .AddInput("公司名称","公司名称","text")
        .AddInput("姓名","姓名","text")
        .AddInput("职务","职务","text")
        .AddInput("手机","手机","text")
        .AddInput("传真","传真","text")
        .AddInput("固话","固话","text")
        .AddInput("Email","Email","text")
        .AddInput("网址","网址","text")
        .AddInput("地址","地址","text")
    End With
    With wb.AddInputGroup("form1","ipg3","备注")
        .AddTextArea("备注").Placeholder = "请输入200字以内的备注"
    End With
    With wb.AddInputGroup("form1","ipg4","名片反面") '文件上传
        .AddUploader("名片反面","",False)
    End With
    With wb.AddButtonGroup("form1","btg1",True)
        .Add("btn1", "确定", "submit")
    End With
    wb.AppendHTML("<script src='./lib/ajaxform.js'></script>") '引入脚本文件
e.WriteString(wb.Build) '生成网页

 

ajaxform.js文件代码如下

function getcard(){
    var result= submitAjaxFileds('bdcardsb.htm','','名片正面',false);
    if(result){
        var vals=result.split("|");
        if(vals.length==6){
            document.getElementById("姓名").value=vals[0];
            document.getElementById("手机").value=vals[1];
     document.getElementById("固话").value=vals[2];
     document.getElementById("Email").value=vals[3];
     document.getElementById("网址").value=vals[4];
     document.getElementById("地址").value=vals[5];
        }
    }
}

 

用bdcardsb.htm测试接收到的文件,测试不到,代码如下

Dim e As RequestEventArgs = args(0)
Dim sb As New StringBuilder
For Each key As String In e.Files.Keys
    sb.AppendLine(key & " 上传" & e.Files(key).Count & "个文件,分别是:</br>")
    For Each fl As String In e.Files(key)
        Dim NewName As String = fl
        Dim idx As Integer = fl.LastIndexOf(".")
        Dim cnt As Integer = 1
        Do While FileSys.FileExists("d:\web\temp\" & NewName) '判断文件夹是否存在同名文件
            NewName = fl.Insert(idx,"(" & cnt & ")") '如果存在同名文件,在原文件名加上序号
            cnt = cnt + 1 '递增序号
        Loop
        sb.AppendLine(fl & " → " & NewName & "<br>") '
        e.SaveFile(key,fl,"d:\web\temp\" & NewName) '保存接收到的文件
    Next
    sb.AppendLine("</br>")
Next
MessageBox.Show(sb.ToString)

显示空的,啥都没有

[此贴子已经被作者于2018/9/14 9:27:42编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/9/14 9:34:00 [只看该作者]

必须把整个表单提交,才可以把图片发送上去的。

 

1、你 submitAjaxFileds 要改成 submitAjaxForm

 

2、要用增强uploader http://www.foxtable.com/mobilehelp/scr/0106.htm

 


 回到顶部
帅哥哟,离线,有人找我吗?
ajie5211
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:四尾狐 帖子:820 积分:5641 威望:0 精华:1 注册:2015/11/28 14:00:00
  发帖心情 Post By:2018/9/14 11:13:00 [只看该作者]

帮我看看怎么没有反应,代码如下

Dim e As RequestEventArgs = args(0)
Dim oid As Integer = args(1)
Dim wb As New weui
If e.PostValues.Count = 0 Then '生成增加订单网页
    wb.AddForm("","form1","cardbookaddnew.htm")
    With wb.AddInputGroup("form1","ipg1","名片正面") '文件上传
        With .AddUploader("名片正面","",True)
            .AllowDelete = True '允许删除
            .Incremental = False '允许重复选择文件或连续拍照
            .ScaleWidth = 400 '自动压缩图片宽度为400个像素,高度等比例压缩
            .Attribute = "  onch an ge= 'upc ar dz()' " '调用js函数
        End With
    End With
    With wb.AddInputGroup("form1","ipg2","名片信息")
        .AddHiddenValue("OperID",oid)
        .AddInput("公司名称","公司名称","text")
        .AddInput("姓名","姓名","text")
        .AddInput("职务","职务","text")
        .AddInput("手机","手机","text")
        .AddInput("传真","传真","text")
        .AddInput("固话","固话","text")
        .AddInput("Email","Email","text")
        .AddInput("网址","网址","text")
        .AddInput("地址","地址","text")
    End With
    With wb.AddInputGroup("form1","ipg3","备注")
        .AddTextArea("备注").Placeholder = "请输入200字以内的备注"
    End With
    With wb.AddInputGroup("form1","ipg4","名片反面") '文件上传
        .AddUploader("名片反面","",False)
    End With
    wb.AddToast("","tst1", "正在上传",1)
    wb.AddToast("","tst2", "上传成功",0)
    wb.AddToast("","tst3", "上传失败",0).Icon= "warn"
    wb.AppendHTML("<script src='./lib/ajaxform.js'></script>") '引入脚本文件   
    With wb.AddButtonGroup("form1","btg1",True)
        .Add("btn1", "确定", "submit")
    End With
    e.WriteString(wb.Build) '生成网页

Else '保存新增的订单
    Dim sb As New StringBuilder
    For Each key As String In e.Files.Keys
        sb.AppendLine(key & " 上传" & e.Files(key).Count & "个文件,分别是:</br>")
        For Each fl As String In e.Files(key)
            Dim NewName As String = fl
            Dim idx As Integer = fl.LastIndexOf(".")
            Dim cnt As Integer = 1
            Do While FileSys.FileExists("d:\web\temp\" & NewName) '判断文件夹是否存在同名文件
                NewName = fl.Insert(idx,"(" & cnt & ")") '如果存在同名文件,在原文件名加上序号
                cnt = cnt + 1 '递增序号
            Loop
            sb.AppendLine(fl & " → " & NewName & "<br>") '
            e.SaveFile(key,fl,"d:\web\temp\" & NewName) '保存接收到的文件
        Next
        sb.AppendLine("</br>")
    Next
    e.WriteString("OK")

End If

 

"ajaxform.js"代码如下

function upcardz(){
    var result = submitAjaxForm('form1','afterSubmit',true,'tst1');
}


function afterSubmit(result){
    hide("tst1");
    if (result=='OK') {
        show("tst2");
        location="cardbookaddnew.htm";
    }
    else{
        show("tst3",2000);
    }
}

[此贴子已经被作者于2018/9/14 11:15:42编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/9/14 12:06:00 [只看该作者]

测试代码

 

        With wb.AddInputGroup("form1","ipg1","名片正面") '文件上传
            With .AddUploader("名片正面","",True)
                .AllowDelete = True '允许删除
                .Incremental = True '允许重复选择文件或连续拍照
                .ScaleWidth = 400 '自动压缩图片宽度为400个像素,高度等比例压缩
                .Attribute = "  " '调用js函数
            End With
        End With
       
        wb.AppendHTML("<script>名片正面.addEventListener('change', function(e){alert(123)},false );</script>") '引入脚本文件

 

在alert那里写上传的代码



 回到顶部
帅哥哟,离线,有人找我吗?
ajie5211
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:四尾狐 帖子:820 积分:5641 威望:0 精华:1 注册:2015/11/28 14:00:00
  发帖心情 Post By:2018/9/14 13:33:00 [只看该作者]

图片点击可在新窗口打开查看整了半天,也不知道这上传代码是个啥。我把JS里的写进来也不行,把哪个内容变动后引用JS写进去也不行,这个真整不明白是个啥子。

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/9/14 14:39:00 [只看该作者]

比如

 

        wb.AppendHTML("<script>名片正面.addEventListener('change', function(e){alert(123);submitAjaxForm('form1','afterSubmit',true,'tst1');},false );</script>") '引入脚本文件

 

或者

 

        wb.AppendHTML("<script>名片正面.addEventListener('change', function(e){alert(123);upcardz();},false );</script>") '引入脚本文件


 回到顶部
帅哥哟,离线,有人找我吗?
ajie5211
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:四尾狐 帖子:820 积分:5641 威望:0 精华:1 注册:2015/11/28 14:00:00
  发帖心情 Post By:2018/9/14 16:03:00 [只看该作者]

整不好,我还是加个按钮来处理了,不过图片上传后,收到的数据怎么写入当前页面,要重新生成页面吗?如果重新生成页面,哪刚刚的图片怎么办是不是就没有了?

我现在的填报代码如下:

Dim e As RequestEventArgs = args(0)
Dim oid As Integer = args(1)
Dim wb As New weui
If e.PostValues.Count = 0 Then '生成增加订单网页
    wb.AddForm("","form1","cardbookaddnew.htm")
    With wb.AddInputGroup("form1","ipg1","名片正面") '文件上传
        With .AddUploader("名片正面","",True)
            .AllowDelete = True '允许删除
            .Incremental = False '允许重复选择文件或连续拍照
        End With
    End With
    With wb.AddButtonGroup("form1","btg2",True)
        .Add("btn2", "解析名片", "button").Attribute= " on cl ick ='up ca rdz()'"
    End With
    With wb.AddInputGroup("form1","ipg2","名片信息")
        .AddHiddenValue("OperID",oid)
        .AddInput("公司名称","公司名称","text")
        .AddInput("姓名","姓名","text")
        .AddInput("职务","职务","text")
        .AddInput("手机","手机","text")
        .AddInput("传真","传真","text")
        .AddInput("固话","固话","text")
        .AddInput("Email","Email","text")
        .AddInput("网址","网址","text")
        .AddInput("地址","地址","text")
    End With
    With wb.AddInputGroup("form1","ipg3","备注")
        .AddTextArea("备注").Placeholder = "请输入200字以内的备注"
        With  .AddUploader("名片反面","名片反面",False)
            .TextPosition = 0 '标题靠左
        End With
    End With
    wb.AddToast("","tst1", "正在上传",1)
    wb.AddToast("","tst2", "上传成功",0)
    wb.AddToast("","tst3", "上传失败",0).Icon= "warn"
    wb.AppendHTML("<script src='./lib/ajaxform.js'></script>") '引入脚本文件
    With wb.AddButtonGroup("form1","btg1",True)
        .Add("btn1", "确定", "submit")
    End With
    e.WriteString(wb.Build) '生成网页
Else '保存新增的订单
    Dim mpzm As String
    For Each key As String In e.Files.Keys
        If  key = "名片正面" Then
            For Each fl As String In e.Files(key)
                Dim NewName As String = fl
                Dim idx As Integer = fl.LastIndexOf(".")
                Dim cnt As Integer = 1
                Do While FileSys.FileExists("d:\web\temp\" & NewName) '判断文件夹是否存在同名文件
                    NewName = fl.Insert(idx,"(" & cnt & ")") '如果存在同名文件,在原文件名加上序号
                    cnt = cnt + 1 '递增序号
                Loop
                e.SaveFile(key,fl,"d:\web\temp\" & NewName) '保存接收到的文件
                mpzm = "d:\web\temp\" & NewName
            Next
        End If
    Next
    mpzm = Functions.Execute("bdcardsb",mpzm)
    e.WriteString(mpzm)
End If

 

ajaxform.js代码如下

function upcardz(){
    var result = submitAjaxForm('form1','afterSubmit',true,'tst1');
}


function afterSubmit(result){
    hide("tst1");
    if (result) {
        var vals=result.split("|");
    if(vals.length==6){
            document.getElementById("姓名").value=vals[0];
            document.getElementById("手机").value=vals[1];
            document.getElementById("固话").value=vals[2];
            document.getElementById("Email").value=vals[3];
            document.getElementById("网址").value=vals[4];
            document.getElementById("地址").value=vals[5];
    }
    }
    else{
        show("tst3",2000);
    }
}
这个刷新不了当前页

Functions.Execute("bdcardsb",mpzm)代码如下:

Dim mpzm As String = args(0)
Dim bmp As new Bitmap(mpzm)
Dim ms As new System.Io.MemoryStream()
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)

Dim arr(ms.Length) As Byte
ms.Position = 0
ms.Read(arr, 0, ms.Length)
ms.Close()

Dim strbase64 As String = Convert.ToBase64String(arr)
bmp.dispose
'****读取图片完毕

Dim body As String = "id_card_side=front&image=" & UrlEncode(strbase64) & ""
Dim token As String = Functions.Execute("百度名片识别AccessToken")
Dim req = System.Net.WebRequest.Create("https://aip.baidubce.com/rest/2.0/ocr/v1/business_card?access_token=" & token)
req.Method = "POST"
req.C
Dim aryBuf As Byte() = Encoding.UTF8.GetBytes(body)
req.ContentLength = aryBuf.Length
Dim writer = req.GetRequestStream()
writer.Write(aryBuf, 0, aryBuf.Length)
writer.Close()
writer.Dispose()
Dim pos = req.GetResponse
'****获取数据完毕

Dim stm As System.IO.Stream = pos.GetResponseStream()
Dim reader As New System.IO.StreamReader(stm)
Dim str As String = reader.ReadToEnd
pos.Close
stm.Close
reader.close

Dim jo As JObject = Jobject.Parse(str)
Dim lms() As String = {"NAME","MOBILE","TEL","EMAIL","URL","ADDR"}
Dim fhstr As String
For Each lm As String In lms
    Dim jt As String = ""
    For Each ja As JToken In jo("words_result")(lm)
        jt = iif(jt > "",jt & "," & ja.ToString,ja.ToString)
    Next
    If fhstr > "" Then
        fhstr = fhstr & "|" & jt
    Else
        fhstr = jt
    End If
Next
Return fhstr

[此贴子已经被作者于2018/9/14 16:03:41编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/9/14 16:26:00 [只看该作者]

1、执行这个返回什么?

 

mpzm = Functions.Execute("bdcardsb",mpzm)

msgbox(mpzm)

 

2、js里面执行 alert(result) 弹出什么?

 


 回到顶部
帅哥哟,离线,有人找我吗?
ajie5211
  9楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:四尾狐 帖子:820 积分:5641 威望:0 精华:1 注册:2015/11/28 14:00:00
  发帖心情 Post By:2018/9/14 16:29:00 [只看该作者]

另外我发现百度名片识别每次返回来的“words_result”包含项不一样,有时{"NAME","MOBILE","TEL","EMAIL","URL","ADDR"}
有时{"NAME","MOBILE",“FAX”,"TEL","EMAIL","URL","ADDR"},有时里面哪一项又会少,这时,如何判断有没有这一项哪?
For Each lm As String In lms
    Dim jt As String = ""
    For Each ja As JToken In jo("words_result")(lm)   “就是这个lm如何判断一下,不然有时没有这一项会报错。
        jt = iif(jt > "",jt & "," & ja.ToString,ja.ToString)
    Next
    If fhstr > "" Then
        fhstr = fhstr & "|" & jt
    Else
        fhstr = jt
    End If
Next

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  10楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/9/14 16:34:00 [只看该作者]

一项一项的获取,不要偷懒用循环。没有的要赋值为空。


 回到顶部
总数 22 1 2 3 下一页