手机摄像头拍摄的照片上传(js .net)

参考:

https://developer.mozilla.org/zh-CN/docs/Web/API/FormData/Using_FormData_Objects

https://segmentfault.com/q/1010000011957065/

https://blog.csdn.net/qq_39327418/article/details/89212987

http://jqweui.com/

http://jqweui.com/download

前端js

后端.net(一般处理程序,ashx)

 

html

  <a href="javascript:;" class="btn btn-success btn-block" data-use="shuidian" data-useid="2"
           onclick="UploadFile('form1')">上传文件</a>

 

 <input type="file" accept="image/*" capture="camera" id="newimage" name="newimage" style="" >

js

复制代码
function UploadFile(formName) {
            var formData = new FormData();
            var input = document.getElementById("newimage");
            var files = input.files;

            formData.append("aa", "123");

            for (var i = 0; i < files.length; i++) {
                formData.append("upfile[]", files[i]);
            }


            $.ajax({
                url: "WebService.ashx",
                type: "post",
                data: formData,
                processData: false,
                contentType: false,
                success: function (res) {
                    if (res) {
                        alert(res);
                    }
                },
                error: function (err) {
                    alert("网络连接失败,稍后重试", err.responseText);
                }

            })

        }
复制代码

ashx

复制代码
public void ProcessRequest(HttpContext context)
        {
            if (context.Request["aa"] == null)
            {
                return;
            }

            if (context.Request["aa"].ToString() != "123")
            {
                return;
            }

            if (context.Request.Files.Count == 0)
            {
                return;
            }

            string rtn = "Ok";

            string aa= string.Empty;
            //FileStream file = (HttpInputStream)context.Request.Files[0].InputStream;
            string fileName = string.Empty;
            string fileLength = string.Empty;
            string fileType = string.Empty;
            string path = string.Empty;

            try
            {
                aa= context.Request["aa"].ToString();
                //FileStream file = (HttpInputStream)context.Request.Files[0].InputStream;
                fileName = context.Request.Files[0].FileName;
                fileLength = context.Request.Files[0].ContentLength.ToString();
                fileType = context.Request.Files[0].ContentType;
                path = context.Server.MapPath(@"Files/" + fileName);

                context.Request.Files[0].SaveAs(path);
            }
            catch (Exception ex)
            {
                rtn = ex.Message + ":" + path;
            }


            context.Response.ContentType = "text/plain";
            context.Response.Write(rtn);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
复制代码

 

posted @   阿日斯兰  阅读(266)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示