手机摄像头拍摄的照片上传(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
前端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; } }